[英]Deserializing JSON to .NET
如何將此類 json 反序列化為 C# 上的對象?
{ "AND":[ { "AND":[ { "AND":[ { "AND":[ { "AND":[ { "AND":[ { "OR":[ { "OR":[ "Title", "Login" ] }, "LoginNote" ] }, "BossTitle" ] }, { "OR":[ "Phone", "TeleGorod" ] } ] }, "Room" ] }, "Division" ] }, "TabelnyiNomer" ] }, "Filter\"" ] }
你可以玩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 的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 應該這樣解析,雖然我沒有測試過。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.