2017-09-28 119 views
-1

如何将此类json反序列化为C#上的对象?将JSON反序列化为.NET

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

.NET是一种格式吗?你需要什么,你有什么尝试? – rmjoia

+0

尝试https://quicktype.io/?l=cs&r=json2csharp – Backs

+2

我认为您需要使用一些已有的.NET库,如'Newtonsoft.Json'。 –

回答

0

你可以玩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); 
     } 
    } 

enter image description here

enter image description here

+0

谢谢! 这并不适用于 串unknownJson工作= “{\” 和\ “:\” _ x041f__x0435__x0440__x0432__x04 \” {\ “和\”:\ “_ x0418__x0437__x0433__x043e__x04 \”{\ “和\”: [\“_ x041e__x043f__x0438__x0441__x04 \”,{\“AND \”:[\“_ x041a__x043e__x0434_ \”,\“Title \”]}]}]}]}“; –

+0

@AntonBobenko它的工作原理。看看答案。我刚刚更新了它。 –

0

quicktype's CLI支持JSON模式为导入格式,它可以帮助你代表你在这里解析树数据结构。我会看看为什么不能自动检测到,但这里的模型:

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 
} 

你的JSON解析应该为这个,虽然我没有测试它。