Reputation: 133
I have below format JSON file which is having different type of values (string, number, boolean and Null). I want to convert this JSON in xml format for some data processing and after that I want to convert back in same JSON format. Needs to insure that it should not loose any data type. Those numbers is in double quotes then it should be same and if some number is in without double quotes then it should be without double quotes.
{
"Source": "WEB",
"CodePlan": 5,
"PlanSelection": "1",
"PlanAmount": "500.01",
"PlanLimitCount": 31,
"PlanLimitAmount": "3000.01",
"Visible": false,
"Count": null
}
Currently i tried to JsonConvert object to serializeObject and DeserializeObject but it looses numerical value and converts everything in double quotes.
Please suggest appropriate way to handle this.
Upvotes: 1
Views: 709
Reputation: 2588
I would do it a little differently than Adam H's answer. First I would declare a class to use:
/* Note I am using string instead of double for PlanAmount and PlanLimitAmount because you have
* those values surrounded by double quotes. If you want to use double instead make sure your JSON does not
* have double quotes around the numbers */
public class MyClass
{
public string Source { get; set; }
public int CodePlan { get; set; }
public string PlanSelection { get; set; }
public string PlanAmount { get; set; }
public int PlanLimitCount { get; set; }
public string PlanLimitAmount { get; set; }
public bool Visible { get; set; }
public int? Count { get; set; }
}
Then using a couple methods you should be able to serialize that class to either JSON or XML:
// You can convert the JSON to an instance of MyClass like this
public MyClass ConvertJsonToMyClass(string json)
{
return JsonConvert.DeserializeObject<MyClass>(json);
}
// You can also convert an instance of MyClass to JSON like this
public string ConvertMyClassToJson(MyClass obj)
{
return JsonConvert.SerializeObject(obj);
}
// You can also serialize your object to XML
public MyClass ConvertMyClassToXML(MyClass obj)
{
System.IO.StringWriter stringWriter = new System.IO.StringWriter();
XmlSerializer serializer = new XmlSerializer(obj.GetType());
serializer.Serialize(stringWriter, obj);
return stringWriter.ToString();
}
Aside from that, you could probably just deserialize your text into an instance of MyClass
and just compare the objects directly instead of serializing to XML and comparing them that way.
Upvotes: 0
Reputation: 1818
Take a look at using JSON.net, you can then use that to convert your JSON to XML
string json = @"{
'@Id': 1,
'Email': '[email protected]',
'Active': true,
'CreatedDate': '2013-01-20T00:00:00Z',
'Roles': [
'User',
'Admin'
],
'Team': {
'@Id': 2,
'Name': 'Software Developers',
'Description': 'Creators of fine software products and services.'
}
}";
XNode node = JsonConvert.DeserializeXNode(json, "Root");
Documentation here
Then after you are done your processing you can convert the XML back to JSON like this:
string xml = @"<?xml version='1.0' standalone='no'?>
<root>
<person id='1'>
<name>Alan</name>
<url>http://www.google.com</url>
</person>
<person id='2'>
<name>Louis</name>
<url>http://www.yahoo.com</url>
</person>
</root>";
XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);
string json = JsonConvert.SerializeXmlNode(doc);
Documentation here
Upvotes: 1