Reputation: 53
Hi I want to convert json from one format to another like below.My Json format is like below I tried using Json.Net plugin.but couldn't find solution
[
{
"bundleKey": "title",
"bundleValue": "Manage cost code",
},
{
"bundleKey": "name",
"bundleValue": "steve",
}]
Want to convert in the following format
[{"title":"Manage cost code"},{"name":"steve"}]
I am tried using following link
JSON Serialize List<KeyValuePair<string, object>>
Upvotes: 0
Views: 2278
Reputation: 129677
Here is a quick way you can use Json.Net to convert your JSON from one format to the other (assuming the input is valid JSON--as you've posted it, there are extra commas after the bundleValues, which I have removed in the code below):
string json = @"
[
{
""bundleKey"": ""title"",
""bundleValue"": ""Manage cost code""
},
{
""bundleKey"": ""name"",
""bundleValue"": ""steve""
}
]";
JArray array = JArray.Parse(json);
JArray outputArray = new JArray();
foreach (JObject item in array)
{
JObject outputItem = new JObject();
outputItem.Add(item["bundleKey"].ToString(), item["bundleValue"]);
outputArray.Add(outputItem);
}
string outputJson = outputArray.ToString(Formatting.None);
Console.WriteLine(outputJson);
Output:
[{"title":"Manage cost code"},{"name":"steve"}]
Upvotes: 3
Reputation: 871
private static List convert(List<Map> jsonNode) {
if(jsonNode == null)
return null;
List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
for (Map json : jsonNode) {
Map tmp = new HashMap();
putVaue(tmp, json);
result.add(tmp);
}
return result;
}
private static void putVaue(Map<String, Object> result,
Map<String, Object> jsonNode) {
String key = (String) jsonNode.get("bundleKey");
Object value = jsonNode.get("bundleValue");
result.put(key, value);
}
Upvotes: 1