Reputation: 709
In my asp.net application , I have to pass data in xml format to stored procedure.
The data which I have is in format of Json as below:
{
"total": 10,
"page": 1,
"records": 100,
"rows": [
{
"id": 0,
"cell": [
"217121",
"1001",
"CAMRY",
"2532",
"2532",
"0",
"36",
"8",
"13",
"0.03",
"0.15",
"0.15",
"0.10",
"0.14"
]
},
{
"id": 1,
"cell": [
"217121",
"1001",
"CAMRY",
"2540",
"2540",
"0",
"6",
"18",
"13",
"0.29",
"0.14",
"0.14",
"0.21",
"0.27"
]
},
{
"id": 2,
"cell": [
"217121",
"1001",
"CAMRY",
"2546",
"2546",
"0",
"9",
"3",
"5",
"0.13",
"0.35",
"0.35",
"0.24",
"-0.32"
]
},
{
"id": 3,
"cell": [
"217121",
"1001",
"CAMRY",
"2548",
"2548",
"0",
"29",
"8",
"14",
"0.30",
"0.16",
"0.16",
"0.23",
"-0.41"
]
},
{
"id": 4,
"cell": [
"217121",
"1001",
"CAMRY",
"2550",
"2550",
"0",
"31",
"17",
"7",
"0.12",
"0.10",
"0.10",
"0.11",
"-0.14"
]
},
{
"id": 5,
"cell": [
"217121",
"1001",
"CAMRY",
"2554",
"2554",
"0",
"20",
"37",
"3",
"0.13",
"0.10",
"0.10",
"0.11",
"-0.62"
]
}
]
}
In my button click I want to parse this and convert this to xml. I am using namespace
using Newtonsoft.Json.Linq;
And used following code,
protected void TEST_Click(object sender, EventArgs e)
{
var modelJson = hdnModelObject.Value;
var obj=JObject.Parse(modelJson);
}
But not sure how to loop it. stuck in this line. Will be great if any suggessions. Regards
Upvotes: 1
Views: 2234
Reputation: 10211
How you can read from documentation on Json.Net
DeserializeXmlNode
The second helper method on JsonConvert is DeserializeXmlNode(). This method takes JSON text and deserializes it into a XmlNode.
Because valid XML must have one root element the JSON passed to DeserializeXmlNode should have one property in the root JSON object. If the root JSON object has multiple properties then the overload that also takes an element name should be used. A root element with that name will be inserted into the deserialized XmlNode.
There is a problem with your json, you need to add a one root element, so you could do something like this .NETFiddle:
This is your current json in string format:
var json =
@"{""total"":10,""page"":1,""records"":100,""rows"":[{""id"":0,""cell"":[""217121"",""1001"",""CAMRY"",""2532"",""2532"",""0"",""36"",""8"",""13"",""0.03"",""0.15"",""0.15"",""0.10"",""0.14""]},{""id"":1,""cell"":[""217121"",""1001"",""CAMRY"",""2540"",""2540"",""0"",""6"",""18"",""13"",""0.29"",""0.14"",""0.14"",""0.21"",""0.27""]},{""id"":2,""cell"":[""217121"",""1001"",""CAMRY"",""2546"",""2546"",""0"",""9"",""3"",""5"",""0.13"",""0.35"",""0.35"",""0.24"",""-0.32""]},{""id"":3,""cell"":[""217121"",""1001"",""CAMRY"",""2548"",""2548"",""0"",""29"",""8"",""14"",""0.30"",""0.16"",""0.16"",""0.23"",""-0.41""]},{""id"":4,""cell"":[""217121"",""1001"",""CAMRY"",""2550"",""2550"",""0"",""31"",""17"",""7"",""0.12"",""0.10"",""0.10"",""0.11"",""-0.14""]},{""id"":5,""cell"":[""217121"",""1001"",""CAMRY"",""2554"",""2554"",""0"",""20"",""37"",""3"",""0.13"",""0.10"",""0.10"",""0.11"",""-0.62""]}]}";
This is your json string wrapped in a root element:
var jsonWithRoot = string.Format("{{'root': {0}}}",json);
This is your XMLDocument:
XmlDocument doc = (XmlDocument)JsonConvert.DeserializeXmlNode(jsonWithRoot);
Console Application example:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml;
using Newtonsoft.Json;
using System.IO;
public class Program
{
public static void Main()
{
var json =
@"{""total"":10,""page"":1,""records"":100,""rows"":[{""id"":0,""cell"":[""217121"",""1001"",""CAMRY"",""2532"",""2532"",""0"",""36"",""8"",""13"",""0.03"",""0.15"",""0.15"",""0.10"",""0.14""]},{""id"":1,""cell"":[""217121"",""1001"",""CAMRY"",""2540"",""2540"",""0"",""6"",""18"",""13"",""0.29"",""0.14"",""0.14"",""0.21"",""0.27""]},{""id"":2,""cell"":[""217121"",""1001"",""CAMRY"",""2546"",""2546"",""0"",""9"",""3"",""5"",""0.13"",""0.35"",""0.35"",""0.24"",""-0.32""]},{""id"":3,""cell"":[""217121"",""1001"",""CAMRY"",""2548"",""2548"",""0"",""29"",""8"",""14"",""0.30"",""0.16"",""0.16"",""0.23"",""-0.41""]},{""id"":4,""cell"":[""217121"",""1001"",""CAMRY"",""2550"",""2550"",""0"",""31"",""17"",""7"",""0.12"",""0.10"",""0.10"",""0.11"",""-0.14""]},{""id"":5,""cell"":[""217121"",""1001"",""CAMRY"",""2554"",""2554"",""0"",""20"",""37"",""3"",""0.13"",""0.10"",""0.10"",""0.11"",""-0.62""]}]}";
var jsonWithRoot = string.Format("{{'root': {0}}}",json);
XmlDocument doc = (XmlDocument)JsonConvert.DeserializeXmlNode(jsonWithRoot);
using (var stringWriter = new StringWriter())
using (var xmlTextWriter = XmlWriter.Create(stringWriter))
{
doc.WriteTo(xmlTextWriter);
xmlTextWriter.Flush();
Console.Write(stringWriter.GetStringBuilder().ToString());
}
}
}
Upvotes: 3
Reputation: 197
You could use JArray to parse your json string, and iterator the array.
JArray a = JArray.Parse(json);
foreach(var obj in a) {
//iterator the array
}
Upvotes: 0