从json动态生成c#类

时间:2022-10-29 13:46:55

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

我通常使用json2csharp为c#生成json类。但我确实有问题。我的json有这样的动态深度

    {
        "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.

那么如何动态生成json类,以便它可以用于我的json?在上面的例子中我有3个深度。但如果我去不同的页面,它可能有4个或更多的深度。

2 个解决方案

#1


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

要反序列化,我将使用Json.Net

 var res = JsonConvert.DeserializeObject<RootObj>(json);

#2


0  

You can always use the Newtonsoft.Json

您始终可以使用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.

PS:不确定WP7上是否支持Newtonsoft.Json。

#1


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

要反序列化,我将使用Json.Net

 var res = JsonConvert.DeserializeObject<RootObj>(json);

#2


0  

You can always use the Newtonsoft.Json

您始终可以使用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.

PS:不确定WP7上是否支持Newtonsoft.Json。