给定数据库结构:
| Context | Resource | TypeCode | Value |
|---------|----------|----------|---------------------|
| Home | Header | | Welcome to the site |
| Home | Footer | | copyright company |
| Error 1 | | | Error! |
| UPC | 55 | Name | Product |
| UPC | 55 | Weight | 10 |
我需要能够生成这个Json:
{
"Home": {
"Header": "Welcome to the site",
"Footer": "copyright company"
},
"Error1": "Error!",
"UPC": {
"55": {
"Name": "Product",
"Weight": "10"
}
}
}
我将使用它将翻译提供给angular-translate。值已设置为目标语言的值。我已经看到了这种序列化的各种例子,但它们都假设目标值的深度一致。
答案 0 :(得分:0)
尝试类似这样的事情
List<List<string>> a = /*parse ur data to this*/;
foreach(var pa in a){
dynamic r = getDynamicObject(pa /*List<string>*/);
string jsonString = Newtonsoft.Json.SerializeObject(r);
/*you might have ur string*/
}
/*separate function*/
GetDynamicObject(IEnumerable<string> a){
dynamic r = new ExpandoObject();
if(a.count > 2){
return (r as Dictionary<string, object>).Add(a.First(), GetDynamicObject(a.Skip(1)));
}
else {
return (r as Dictionary<string, object>).Add(a.First(), a.Last());
}
}
注意: - 我在这里编写此代码只能包含编译时错误。