简体   繁体   中英

Deserializing JSON to .NET

How to deserialize json of this kind into an object on 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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