我有一个这样的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
}]
}
两者都是有效的 JSON,提供相同的数据,但我的应用程序只能读取后面的数据。所以,我必须将第一个转换为第二个。我有很多这样的数据。我怎样才能务实地做到这一点,或者有什么解决方法吗?
必须有括号 "{}"
{ "row_elements":[ { "级别":"[产品]。[公司]。[公司]", "showGroupTotal":假 },
{ "级别":"[产品]。[国际产品名称]。[国际产品名称]", "showGroupTotal":假 }, { "级别":"[产品]。[本地产品名称]。[本地产品名称]", "showGroupTotal":假 }] }
试试这个:
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
}
]
}