Skip to content Skip to sidebar Skip to footer

Convert Json String Into Meaning Full Data To Parse

ok so i have this string: [{'id':1},{'id':2,'children':[{'id':3},{'id':4,'children':[{'id':5,'children':[{'id':6}]}]},{'id':7}]}] I would like to be able to parse it and use it as

Solution 1:

OP changed the question, so the following is based on previous question:

If you control the output, then you should use XML as your transfer language. It is VERY simple to use and there is built in support in C# for it.

Your result go from this:

{"id":1},{"id":2->"children":{"id":3},{"id":4->"children":{"id":5->"children":{"id":6}}},{"id":7}}

Would become:

<root><itemid="1" /><itemid="2"><itemid="3" /><itemid="4"><itemid="5"><itemid="6" /></item></item><itemid="7" /></item></root>

Then you can read it with:

XElementroot= XElement.Parse(xml); // or .Load(file)
Dictionary<int,int?> list = root.Descendants("item")
    .ToDictionary(x => (int)x.Attribute("id"), x => 
    {
        varparentId= x.Parent.Attribute("id");
        if (parentId == null)
            returnnull;
        return (int)parentId;
    });

Now you have a dictionary list of key value pairs just as you wanted with

ID | ParentID
------------------------
1     NULL
2     NULL
3      2
4      2
5      4
6      5
7      2

=== Convert Back ===

Dictionary<int, int?> dic = newDictionary<int, int?>
{
    {1,null},
    {2,null},
    {3,2},
    {4,2},
    {5,4},
    {6,5},
    {7,2}
};

XElementroot=newXElement("root");
foreach (var kvp in dic)
{
    XElementnode=newXElement("item", newXAttribute("id", kvp.Key));

    int? parentId = kvp.Value;
    if (null == parentId)
        root.Add(node);
    else
    {
        // Get first item with id of parentIdXElementparent= root.Descendants("item")
            .FirstOrDefault(i => (int)i.Attribute("id") == (int)parentId);
        if (null != parent) // which it shouldn't for our array
            parent.Add(node);
    }
}

To get to a string, use:

string xml = root.ToString();

Or to save to a file:

root.Save("filepath");

Solution 2:

You could just deserialize it into a class and easely extract the data from there.

Note that System.Web.Script.Serialization.JavaScriptSerializer is in System.Web.Extensions

[Serializable()]
publicclassInfo
{
 private int _id;

 privateList<Info> _children;
 public int Id {
  get { return _id; }
  set { _id = value; }
 }

 publicList<Info> Children {
  get { return _children; }
  set { _children = value; }
 }

}


public void Main()
{
 string json = null;
 List<Info> result = null;
 System.Web.Script.Serialization.JavaScriptSerializer ser = new System.Web.Script.Serialization.JavaScriptSerializer();

 json ="[{\"id\":1},{\"id\":2,\"children\":[{\"id\":3},{\"id\":4,\"children\":[{\"id\":5,\"children\":[{\"id\":6}]}]},{\"id\":7}]}]";

 result = ser.Deserialize<List<Info>>(json);

 foreach (Info p in result) {
  Console.WriteLine(p.Id);

  if (p.Children!= null) {
   foreach (Info c in p.Children) {
    Console.WriteLine("   "+ c.Id);
   }
  }
 }

 Console.ReadLine();

}

Solution 3:

Using Json.NET, you can simply pass your string in to get a JArray of JObjects:

JArrayarr= JArray.Parse(yourString)

You can then use LINQ as you would on any LINQ-supporting collection.

Post a Comment for "Convert Json String Into Meaning Full Data To Parse"