1

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.

user2478625
  • 123
  • 2
  • 12

2 Answers2

1

Take a look at using JSON.net, you can then use that to convert your JSON to XML

string json = @"{
  '@Id': 1,
  'Email': 'james@example.com',
  '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

Adam H
  • 1,750
  • 1
  • 9
  • 24
  • My JSON packet is not having fix format. We get different type of JSON packet in run time so unable to create any fix class format. – user2478625 Jul 23 '18 at 06:58
  • @user2478625 This solution does not require a 'fixed' class. Everything is placed in dynamic/anonymous objects. – Adam H Jul 23 '18 at 14:20
0

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.

Omar Himada
  • 2,540
  • 1
  • 14
  • 31
  • This is a solid solution as well, the only change I would potentially make is instead of a class I'd use a struct but that's a very small thing to even consider. – Adam H Jul 19 '18 at 18:41
  • Thanks O H. Actually in our case JSON format is not fix, every times it comes in different format as a input so I am not sure if there is any option where we can create class file dynamically. – user2478625 Jul 20 '18 at 13:20