反序列化嵌套的json对象c#

时间:2016-05-09 09:48:52

标签: c# json

我有一个嵌套的json对象,就像这样,

  "where": {
      "operator": "AND",
      "left": {
        "operator": "=",
        "$fieldRef": "requestor",
        "value": "@me"
      },
      "right": {
        "operator": "=",
        "$fieldRef": "state",
        "value": "Closed"
      }
    },

我想在C#中反序列化,但问题是对象可以根据用户更大,对象可以如下,

甚至是这样,

"where": {
      "operator": "OR",
      "left": {
        "operator": "STARTSWITH",
        "$fieldRef": "id"
      },
      "right": {
        "operator": "OR",
        "left": {
          "operator": "STARTSWITH",
          "$fieldRef": "orgId"
        },
        "right": {
          "operator": "AND",
          "left": {
            "operator": "STARTSWITH",
            "$fieldRef": "firstname"
          },
          "right": {
            "operator": "STARTSWITH",
            "$fieldRef": "lastname"
          }
        }
      }
    },

1 个答案:

答案 0 :(得分:1)

请考虑使用最受欢迎的图书馆:json.NET

然后,您可以使用内置的Visual Studio解析工具将JSON用于以下类中找到的类:EDIT - >粘贴特殊 - >将JSON粘贴为类。 这将生成json所需的类。

一旦你添加了对json.NET的引用,你就可以运行以下命令来反序列化:

var query = (from attendance in db.Attendances
             where attendance.CreateDate.Value.Year >= 2016
             select new
             {
                 attendance.Employee,
                 attendance.EmpNTLogin
             }).Distinct().OrderBy(att => att.EmpNTLogin);

" RootObject"是你json结构中最顶级的。