0

I usually use json2csharp to generate json classes to c#. But I do have problem. My json is have dynamic depth like this

    {
        "kategori": [
            {
                "id": "1",
                "namakategori": "Tips & Trick",
                "parent_id": "0",
                "children": [
                    {
                        "id": "348",
                        "namakategori": "Fotografi",
                        "parent_id": "1",
                        "children": []
                    },
                    {
                        "id": "370",
                        "namakategori": "Hacking",
                        "parent_id": "1",
                        "children": []
                    }
                ]
            },
     {
                "id": "12",
                "namakategori": "Aplikasi",
                "parent_id": "0",
                "children": [
                    {
                        "id": "13",
                        "namakategori": "Tools",
                        "parent_id": "12",
                        "children": [
                                     {
                                       "id": "14",
                                      "namakategori": "Toolsorder",
                                       "parent_id": "13",
                                       "children":[]
                                      },
                                     ]
                             },
                          ]
                       },
             ]
}

So how do I generate json classes dynamically so it can be used for my json? In above example I have 3 depth. But if I go to different page maybe it have 4 or more depth.

2
  • Shouldn't one class be enough? Something like class jsonClass { int id { get; set; } string namakategori { get; set; } int parent_id { get; set; } List<jsonClass> children { get; set ; } jsonClass() { children = new List<jsonClass>(); } } Commented Mar 31, 2014 at 14:22
  • What If I want to binding child only from that json? Or if I want to binding parent json only? From those class, I only can get child and parent become one class and I can't find how to distinct between parent and child data Commented Apr 1, 2014 at 6:42

2 Answers 2

1

You don't need to declere your classes dynamically. This should work:

public class Child
{
    public string id { get; set; }
    public string namakategori { get; set; }
    public string parent_id { get; set; }
    public List<Child> children { get; set; } // <-- See this
}

public class RootObj
{
    public List<Child> kategori { set; get; }
}

To deserialize I'll use Json.Net

 var res = JsonConvert.DeserializeObject<RootObj>(json);
Sign up to request clarification or add additional context in comments.

9 Comments

What If I want to binding child only from that json? Or if I want to binding parent json only? From those class, I only can get child and parent become one class and I can't find how to distinct between parent and child data
@blueboyz because there is no distinction. Every child is a parent of another child(s). Consider it like a tree. You should traverse your tree to find the required node.
Any example code? maybe how I parse it. I still can't understand what you mean. What I need is to binding parent and child differently so I can display it inside listbox which have format parent and child differently
@blueboyz if you could do what you wanted, you wouldn't have asked that question. The answer is above. How to traverse a tree (using a stack or recursive functions) is a different question (and out of scope of this).
Can it be binded to listbox? I want later those class can be binded to listbox which have parent and child differently
|
0

You can always use the Newtonsoft.Json

For Instance,

JObject result = (JObject) JsonConvert.DeserializeObject(yourJsonDataHere);
var katObject = result.Property("kategori").Value;

and so on...

PS: Not sure if Newtonsoft.Json is supported on WP7.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.