Convert JSON to XML Class

I am unable to align Json class in Linq XML.

Elements c.first

, c.second

and are c.third

highlighted and indicate:

"You are missing a directive or assembly reference."

var serializer = new JavaScriptSerializer();
var json1 = "[count:[place:{first:1,second:2,third:3}],[place:{first:11,second:22,third:33}],[place:{first:111,second:222,third:333}]]]";
var jsons = serializer.Serialize(json1);
var jsona = serializer.Deserialize<List<jClass>>(jsons);
var xmld = new XDocument(
    new XElement("count", jsona.Select(c =>
        new XElement("place",
            new XElement("first", c.first),
            new XElement("second", c.second),
            new XElement("third", c.third)
        )
    ))
);

      

Class.cs

public class jClass
{
    public jNumber[] count { get; set; }
}
public class jNumber
{
    public jTopThree[] place { get; set; }
}
public class jTopThree
{
    public int first { get; set; }
    public int second { get; set; }
    public int third { get; set; }
}

      

+3


source to share


4 answers


Your problem is that your object structure is essentially an array of arrays of arrays and you only make one selection. When you build your xml your variable c

is at the level jClass

, so you are trying to read the properties first

, second

and third

.



It's not clear what your xml structure should be, but you will have to use a couple more .Select

calls to expand further to instances of jTopThree, use .SelectMany

to flatten it or change object definitions a bit.

+2


source


The json object text and structure seems to be invalid. you can try something like this.

Object structure

public class jClass
{
    public jTopThree[] count { get; set; }
}
public class jTopThree
{
    public int first { get; set; }
    public int second { get; set; }
    public int third { get; set; }
}

      



serialization

        var serializer = new JavaScriptSerializer();
        var json1 = "{count:[{first:1,second:2,third:3},{first:11,second:22,third:33},{first:111,second:222,third:333}]}";
        var obj = serializer.Deserialize<jClass>(json1);
        XmlSerializer xmlSerializer = new XmlSerializer(typeof(jClass));
        string xmlString;
        using (System.IO.StringWriter sww = new System.IO.StringWriter())
        {
            using (XmlWriter writer = XmlWriter.Create(sww))
            {
                xmlSerializer.Serialize(writer, obj);
                xmlString = sww.ToString();
            }
        }

      

0


source


        var json1 = "[{\"place\":{\"first\":1,\"second\":2,\"third\":3}},{\"place\":{\"first\":11,\"second\":22,\"third\":33}},{\"place\":{\"first\":111,\"second\":222,\"third\":333}}]";
        XmlDocument deserializeXmlNode;
        if (json1.IndexOf("[", StringComparison.OrdinalIgnoreCase) == 0)
        {
            deserializeXmlNode = Newtonsoft.Json.JsonConvert.DeserializeXmlNode($"{{\"items\":{json1}}}", "source");
        }
        else
        {
            deserializeXmlNode = Newtonsoft.Json.JsonConvert.DeserializeXmlNode(json1, "source");
        }

        Console.WriteLine(deserializeXmlNode.OuterXml);

      

If you just want to copy to XML and can afford the Nugget package, go to Newtonsoft.Json and modify the above code to suit your needs. I have slighlty changed the json input because it is not valid for http://www.json.org/ . It should work with any valid JSON though.

http://www.newtonsoft.com/json/help/html/ConvertingJSONandXML.htm

How can I convert JSON to XML or XML to JSON?

0


source


So first, your JSON is poorly formatted, based on your example, I think it should look like this (I think the "space" seems overkill, but I left it for the moment):

{
"count": [
{"place": [
{"first": 1, "second": 2, "third": 3}]},
{"place": [
{"first": 11, "second" : 22, "third": 33}]},
{"place": [
{"First": 111, "second": 222, "third": 333}]}
]
}

Your classes should look like this:

public class RootObject
{
    public List<Count> count { get; set; }
}

public class Count
{
    public List<Place> place { get; set; }
}

public class Place
{
    public int first { get; set; }
    public int second { get; set; }
    public int third { get; set; }
}

      

Finally, you can create XML like this:

var serializer = new JavaScriptSerializer();
var json = "your json string";
var result= serializer.Deserialize<RootObject>(json);
var xmld = new XDocument(
           new XElement("count", result.count
               .SelectMany(p => p.place)
               .Select(x => 
                   new XElement("place",
                       new XElement("first", x.first),
                       new XElement("second", x.second),
                       new XElement("third", x.third)
                   )
                ))
           );

      

Output:

<count>
  <place>
    <first>1</first>
    <second>2</second>
    <third>3</third>
  </place>
  <place>
    <first>11</first>
    <second>22</second>
    <third>33</third>
  </place>
  <place>
    <first>111</first>
    <second>222</second>
    <third>333</third>
  </place>
</count>

      

0


source







All Articles