简体   繁体   English

将 JSON 反序列化为 .NET

[英]Deserializing JSON to .NET

How to deserialize json of this kind into an object on C# ?如何将此类 json 反序列化为 C# 上的对象?

{
   "AND":[
      {
         "AND":[
            {
               "AND":[
                  {
                     "AND":[
                        {
                           "AND":[
                              {
                                 "AND":[
                                    {
                                       "OR":[
                                          {
                                             "OR":[
                                                "Title",
                                                "Login"
                                             ]
                                          },
                                          "LoginNote"
                                       ]
                                    },
                                    "BossTitle"
                                 ]
                              },
                              {
                                 "OR":[
                                    "Phone",
                                    "TeleGorod"
                                 ]
                              }
                           ]
                        },
                        "Room"
                     ]
                  },
                  "Division"
               ]
            },
            "TabelnyiNomer"
         ]
      },
      "Filter\""
   ]
}

You can play with dynamic type, but I don't think it can help you.你可以玩dynamic类型,但我认为它对你没有帮助。

class Program
    {
        static void Main(string[] args)
        {
            string unknownJson1 = "{\r\n  \"Id\": \"1e4495d3-4cd1-4bf2-9da6-4acee2f7a70e\",\r\n  \"Customers\": [\r\n    \"Alice\",\r\n    \"Bob\",\r\n    \"Eva\"\r\n  ]\r\n}";
            string unknownJson2 = "{\"AND\": [\"_ x041f__x0435__x0440__x0432__x04\", {\"AND\": [\"_ x0418__x0437__x0433__x043e__x04\", {\"AND\": [\"_ x041e__x043f__x0438__x0441__x04\", {\"AND\": [\"_ x041a__x043e__x0434_\", \"Title\"]}]}]}] } ";

            JsonSerializer serializer = new JsonSerializer();
            dynamic deserializedObject;

            using (var stringReader = new StringReader(unknownJson2))
            {
                using (var jsonReader = new JsonTextReader(stringReader))
                {
                    deserializedObject = serializer.Deserialize(jsonReader);
                }
            }

            Console.ReadKey(true);
        }
    }

在此处输入图片说明

在此处输入图片说明

quicktype's CLI supports JSON schema as an import format, which could help you represent the tree data structure you're parsing here. quicktype 的CLI 支持 JSON 模式作为导入格式,这可以帮助您表示您在此处解析的树数据结构。 I'll look into why it wasn't detected automatically, but here's your model:我会研究为什么它没有被自动检测到,但这是你的模型:

public class Tree
{
    [JsonProperty("AND")]
    public Leaf[] And { get; set; } // Could be null

    [JsonProperty("OR")]
    public Leaf[] Or { get; set; } // Could be null
}

public struct Leaf
{
    public string String; // Could be null
    public Top Tree; // Could be null
}

Your JSON should parse as this, although I haven't tested it.你的 JSON 应该这样解析,虽然我没有测试过。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM