我有这样的JSON,
{
"row_elements": [
"[Product].[Corporation].[Corporation]",
"[Product].[Int Product Name].[Int Product Name]",
"[Product].[Local Product Name].[Local Product Name]"
]
}
我想将其格式化为,
{ "row_elements": [
{
"level": "[Product].[Corporation].[Corporation]",
"showGroupTotal": false
},
{
"level": "[Product].[Int Product Name].[Int Product Name]",
"showGroupTotal": false
},
{
"level": "[Product].[Local Product Name].[Local Product Name]",
"showGroupTotal": false
}]
}
两者都是有效的jsons,提供相同的数据,但我的应用程序只能读取后一个。所以,我必须将第一个转换为第二个。我有很多像这样的数据。我如何务实地做到这一点,还是有任何解决方法?
答案 0 :(得分:0)
您必须拥有方括号“{}”
{ “row_elements”:[ { “level”:“[Product]。[Corporation]。[Corporation]”, “showGroupTotal”:false },
{ “level”:“[Product]。[Int Product Name]。[Int Product Name]”, “showGroupTotal”:false }, { “level”:“[Product]。[Local Product Name]。[Local Product Name]”, “showGroupTotal”:false }] }
答案 1 :(得分:0)
试试这个:
string json = @"
{
""row_elements"": [
""[Product].[Corporation].[Corporation]"",
""[Product].[Int Product Name].[Int Product Name]"",
""[Product].[Local Product Name].[Local Product Name]""
]
}";
JObject rootObj = JObject.Parse(json);
JArray array = new JArray();
foreach (JToken token in rootObj["row_elements"].Children())
{
JObject item = new JObject();
item.Add("level", token);
item.Add("showGroupTotal", false);
array.Add(item);
}
JObject output = new JObject();
output.Add("row_elements", array);
string newJson = output.ToString();
Console.WriteLine(newJson);
输出:
{
"row_elements": [
{
"level": "[Product].[Corporation].[Corporation]",
"showGroupTotal": false
},
{
"level": "[Product].[Int Product Name].[Int Product Name]",
"showGroupTotal": false
},
{
"level": "[Product].[Local Product Name].[Local Product Name]",
"showGroupTotal": false
}
]
}