解析复杂的JSON而没有反射的对象? [英] Parsing Complex JSON without reflected objects?

查看:106
本文介绍了解析复杂的JSON而没有反射的对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一个API,该API输出具有许多属性(包括子对象)的JSON对象.到目前为止,我在C#中为JSON找到的所有示例都假定.Net对象已被序列化为JSON,并且可以反序列化回.Net对象.

I am using an API that outputs JSON objects with many attributes including sub-objects. So far all of the examples that I have found for JSON in C# assume that the .Net object has been serialized to JSON and can be deserialized back into the .Net object.

我试图找到一种解决方案,可以将JSON读取到某个通用对象中,然后可以通过从通用对象中选择值来为我的业务对象属性手动分配值.

I am trying to find a solution where I can read the JSON into some generic object that I can then manually assign values to my Business Object properties by selecting the values from the generic objects.

可能类似于ADO.Nets Datables和DataRows,您可以在其中将列名称传递给DataRow对象并获取列的值.这可能有点太先进了,但是基本上我希望能够通过遍历它们来读取JSON对象,并通过传递属性名称来从对象中选择值.这意味着我不会将所有属性反序列化到我的业务对象中,而只是从JSON对象中获取特定的属性,并将它们的值分配给我的业务对象的属性.

Possibly something like ADO.Nets Datables and DataRows where you can pass the column names to the DataRow objects and get the values of the columns. This may be a bit too advanced but basically I want to be able to read the JSON objects by iterating through them and select values from the objects by passing the attribute names. This means that I won't be deserializing all of the attributes into my business objects, I will just be taking specific attributes from the JSON objects and assigning their values to the properties of my business objects.

有什么好的解决方案?

以下是从API输出的两个对象的示例:

Here is an example of two objects output from the API:

{
  "appt": [
    {
      "d": 1331717080000, 
      "id": "112115", 
      "inv": [
        {
          "comp": [
            {
              "alarm": [
                {
                  "action": "DISPLAY", 
                  "desc": [
                    {}
                  ], 
                  "trigger": [
                    {
                      "rel": [
                        {
                          "m": 5, 
                          "neg": true, 
                          "related": "START"
                        }
                      ]
                    }
                  ]
                }
              ], 
              "apptId": "112115", 
              "calItemId": "112115", 
              "ciFolder": "112111", 
              "class": "PUB", 
              "compNum": 0, 
              "d": 1331717080000, 
              "desc": [
                {
                  "_content": ""
                }
              ], 
              "descHtml": [
                {
                  "_content": "<html><body></body></html>"
                }
              ], 
              "e": [
                {
                  "d": "20120314T130000", 
                  "tz": "Pacific/Auckland", 
                  "u": 1331683200000
                }
              ], 
              "fb": "B", 
              "fba": "B", 
              "isOrg": true, 
              "loc": "", 
              "method": "PUBLISH", 
              "name": "Test Appt", 
              "noBlob": true, 
              "or": {
                "a": "liam@url.com", 
                "d": "Liam Smith", 
                "url": "liam@url.com"
              }, 
              "rsvp": false, 
              "s": [
                {
                  "d": "20120314T090000", 
                  "tz": "Pacific/Auckland", 
                  "u": 1331668800000
                }
              ], 
              "seq": 0, 
              "status": "CONF", 
              "transp": "O", 
              "uid": "aa2e4fe4-c636-4660-ae92-3e7041fe5669", 
              "url": "", 
              "x_uid": "aa2e4fe4-c636-4660-ae92-3e7041fe5669"
            }
          ], 
          "compNum": 0, 
          "id": 112114, 
          "seq": 0, 
          "type": "appt", 
          "tz": [
            {
              "daylight": [
                {
                  "hour": 2, 
                  "min": 0, 
                  "mon": 9, 
                  "sec": 0, 
                  "week": -1, 
                  "wkday": 1
                }
              ], 
              "dayname": "NZDT", 
              "dayoff": 780, 
              "id": "Pacific/Auckland", 
              "standard": [
                {
                  "hour": 3, 
                  "min": 0, 
                  "mon": 4, 
                  "sec": 0, 
                  "week": 1, 
                  "wkday": 1
                }
              ], 
              "stdname": "NZST", 
              "stdoff": 720
            }
          ]
        }
      ], 
      "l": "112111", 
      "rev": 72672, 
      "s": 0, 
      "uid": "aa2e4fe4-c636-4660-ae92-3e7041fe5669"
    }, 
    {
      "d": 1331717176000, 
      "id": "112117", 
      "inv": [
        {
          "comp": [
            {
              "alarm": [
                {
                  "action": "DISPLAY", 
                  "desc": [
                    {}
                  ], 
                  "trigger": [
                    {
                      "rel": [
                        {
                          "m": 5, 
                          "neg": true, 
                          "related": "START"
                        }
                      ]
                    }
                  ]
                }
              ], 
              "apptId": "112117", 
              "calItemId": "112117", 
              "ciFolder": "112111", 
              "class": "PUB", 
              "compNum": 0, 
              "d": 1331717176000, 
              "desc": [
                {
                  "_content": ""
                }
              ], 
              "descHtml": [
                {
                  "_content": "<html><body></body></html>"
                }
              ], 
              "e": [
                {
                  "d": "20120315T120000", 
                  "tz": "Pacific/Auckland", 
                  "u": 1331766000000
                }
              ], 
              "fb": "B", 
              "fba": "B", 
              "isOrg": true, 
              "loc": "", 
              "method": "PUBLISH", 
              "name": "test 2", 
              "noBlob": true, 
              "or": {
                "a": "liam@url.com", 
                "d": "Liam Smith", 
                "url": "liam@url.com"
              }, 
              "rsvp": false, 
              "s": [
                {
                  "d": "20120315T100000", 
                  "tz": "Pacific/Auckland", 
                  "u": 1331758800000
                }
              ], 
              "seq": 0, 
              "status": "CONF", 
              "transp": "O", 
              "uid": "4081f711-9742-42bd-a5dc-8bc9ddc3305d", 
              "url": "", 
              "x_uid": "4081f711-9742-42bd-a5dc-8bc9ddc3305d"
            }
          ], 
          "compNum": 0, 
          "id": 112116, 
          "seq": 0, 
          "type": "appt", 
          "tz": [
            {
              "daylight": [
                {
                  "hour": 2, 
                  "min": 0, 
                  "mon": 9, 
                  "sec": 0, 
                  "week": -1, 
                  "wkday": 1
                }
              ], 
              "dayname": "NZDT", 
              "dayoff": 780, 
              "id": "Pacific/Auckland", 
              "standard": [
                {
                  "hour": 3, 
                  "min": 0, 
                  "mon": 4, 
                  "sec": 0, 
                  "week": 1, 
                  "wkday": 1
                }
              ], 
              "stdname": "NZST", 
              "stdoff": 720
            }
          ]
        }
      ], 
      "l": "112111", 
      "nextAlarm": 1331758500000, 
      "rev": 72674, 
      "s": 0, 
      "uid": "4081f711-9742-42bd-a5dc-8bc9ddc3305d"
    }
  ]
}

推荐答案

如果您使用的是 Json.Net 您可以使用dynamic关键字.这是使用您的json字符串的示例代码.

If your are using Json.Net you can make use of dynamic keyword. Here is a sample code using your json string.

dynamic obj = JsonConvert.DeserializeObject(jsonstr);
Console.WriteLine("{0} {1}", obj.appt[0].inv[0].comp[0].method, obj.appt[1].id);

foreach (var appt in obj.appt)
{
    Console.WriteLine("{0} {1}", appt.inv[0].comp[0].method, appt.id);
}

如果您使用的是framerork< 4.0,也可以使用更丑陋的方式

in case you are using a framerork < 4.0, an uglier usage is also possible

JObject obj = (JObject)JsonConvert.DeserializeObject(jsonstr);
foreach (var appt in obj["appt"])
{
    Console.WriteLine("{0} {1}", appt["inv"][0]["comp"][0]["method"], appt["id"]);
}

这篇关于解析复杂的JSON而没有反射的对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆