簡體   English   中英

如何反序列化復雜的 JSON 並將其創建為 C# Object

[英]How to Deserialize complex JSON and create it as C# Object

我有一個 JSON,如下所示:

{
    "Values": [
        {
            "MsgSource": null,
            "TagName": "Data.New_MSG",
            "RawValue": "[\r\n  {\r\n    \"ID\": 145,\r\n    \"StationNo\": 6,\r\n    \"RunTime\": 1800,\r\n    \"ControllerID\": 4,\r\n    \"ControllerAddress\": 2,\r\n    \"ProgramNo\": 2,\r\n    \"ModeID\": \"AutoProgram\",\r\n    \"EventDate\": \"2022-04-27T23:30:02\",\r\n    \"Description\": \"Irrigation Completed\",\r\n    \"MessageCode\": 5\r\n  },\r\n  {\r\n    \"ID\": 144,\r\n    \"StationNo\": 18,\r\n    \"RunTime\": 1800,\r\n    \"ControllerID\": 4,\r\n    \"ControllerAddress\": 2,\r\n    \"ProgramNo\": 5,\r\n    \"ModeID\": \"AutoProgram\",\r\n    \"EventDate\": \"2022-04-27T22:00:00\",\r\n    \"Description\": \"Irrigation Completed\",\r\n    \"MessageCode\": 5\r\n  },\r\n  {\r\n    \"ID\": 143,\r\n    \"StationNo\": 15,\r\n    \"RunTime\": 1800,\r\n    \"ControllerID\": 4,\r\n    \"ControllerAddress\": 2,\r\n    \"ProgramNo\": 4,\r\n    \"ModeID\": \"AutoProgram\",\r\n    \"EventDate\": \"2022-04-27T22:00:02\",\r\n    \"Description\": \"Irrigation Completed\",\r\n    \"MessageCode\": 5\r\n  },\r\n  {\r\n    \"ID\": 142,\r\n    \"StationNo\": 19,\r\n    \"RunTime\": 1800,\r\n    \"ControllerID\": 4,\r\n    \"ControllerAddress\": 2,\r\n    \"ProgramNo\": 5,\r\n    \"ModeID\": \"AutoProgram\",\r\n    \"EventDate\": \"2022-04-27T22:30:02\",\r\n    \"Description\": \"Irrigation Completed\",\r\n    \"MessageCode\": 5\r\n  }\r\n]",
            "ScaledValue": "[\r\n  {\r\n    \"ID\": 145,\r\n    \"StationNo\": 6,\r\n    \"RunTime\": 1800,\r\n    \"ControllerID\": 4,\r\n    \"ControllerAddress\": 2,\r\n    \"ProgramNo\": 2,\r\n    \"ModeID\": \"AutoProgram\",\r\n    \"EventDate\": \"2022-04-27T23:30:02\",\r\n    \"Description\": \"Irrigation Completed\",\r\n    \"MessageCode\": 5\r\n  },\r\n  {\r\n    \"ID\": 144,\r\n    \"StationNo\": 18,\r\n    \"RunTime\": 1800,\r\n    \"ControllerID\": 4,\r\n    \"ControllerAddress\": 2,\r\n    \"ProgramNo\": 5,\r\n    \"ModeID\": \"AutoProgram\",\r\n    \"EventDate\": \"2022-04-27T22:00:00\",\r\n    \"Description\": \"Irrigation Completed\",\r\n    \"MessageCode\": 5\r\n  },\r\n  {\r\n    \"ID\": 143,\r\n    \"StationNo\": 15,\r\n    \"RunTime\": 1800,\r\n    \"ControllerID\": 4,\r\n    \"ControllerAddress\": 2,\r\n    \"ProgramNo\": 4,\r\n    \"ModeID\": \"AutoProgram\",\r\n    \"EventDate\": \"2022-04-27T22:00:02\",\r\n    \"Description\": \"Irrigation Completed\",\r\n    \"MessageCode\": 5\r\n  },\r\n  {\r\n    \"ID\": 142,\r\n    \"StationNo\": 19,\r\n    \"RunTime\": 1800,\r\n    \"ControllerID\": 4,\r\n    \"ControllerAddress\": 2,\r\n    \"ProgramNo\": 5,\r\n    \"ModeID\": \"AutoProgram\",\r\n    \"EventDate\": \"2022-04-27T22:30:02\",\r\n    \"Description\": \"Irrigation Completed\",\r\n    \"MessageCode\": 5\r\n  }\r\n]",
            "Status": "Normal",
            "ComStatus": null,
            "TimeStamp": "2022-04-28 13:17:39.851"
        }
    ]
}

我如何反序列化此 JSON 並創建一個僅包含 RawValue 內部值的列表,其中其中的每個有效負載都將作為列表中的單個元素。 還有如何從列表中的每個元素中刪除不需要的 \r\n 和 escaping 個字符。

你有 JSON 包含 JSON,基本上 - 所以你應該期望必須反序列化一次。 (如果你能改變 JSON 的結構來避免這種雙重序列化,那會更好,誠然,但我會假設這是固定的。)

例如,您可以:

public class Root
{
    public List<Value> Values { get; set; }
}

public class Value
{
    // Add other properties if you need them
    public string RawValue { get; set; }
}

然后:

string json = ...;
Root root = JsonConvert.DeserializeObject<Root>(json);
// This just takes the first value - we don't know whether you actually
// ever have more than one...
string rawValue = root.Values[0].RawValue;
JArray array = JArray.Parse(rawValue);

這假設您樂於將JArray / JObject用於“嵌入式”對象。 如果你也想要 model 那些,你將擁有:

public class Station
{
    [JsonProperty("ID")]
    public int Id { get; set; }
    public int StationNo { get; set; }
    // etc
}

...然后進行反序列化:

string json = ...;
Root root = JsonConvert.DeserializeObject<Root>(json);
// This just takes the first value - we don't know whether you actually
// ever have more than one...
string rawValue = root.Values[0].RawValue;
List<Station> stations = JsonConvert.DeserializeObject<List<Station>>(rawValue);

當你反序列化兩次時,不應該有任何“額外的”escaping。

嘗試這個

List<RawValue> rawValue= JsonConvert.DeserializeObject<List<RawValue>>( 
                       (string) JObject.Parse(json)["Values"]
                       .SelectMany(x =>((JObject) x).Properties()
                       .Where(x=>x.Name=="RawValue")).First()
                       .Value);

結果(json 格式)

[{"ID":145,"StationNo":6,"RunTime":1800,"ControllerID":4,"ControllerAddress":2,"ProgramNo":2,"ModeID":"AutoProgram","EventDate":"2022-04-27T23:30:02","Description":"Irrigation Completed","MessageCode":5},
{"ID":144,"StationNo":18,"RunTime":1800,"ControllerID":4,"ControllerAddress":2,"ProgramNo":5,"ModeID":"AutoProgram","EventDate":"2022-04-27T22:00:00","Description":"Irrigation Completed","MessageCode":5},
{"ID":143,"StationNo":15,"RunTime":1800,"ControllerID":4,"ControllerAddress":2,"ProgramNo":4,"ModeID":"AutoProgram","EventDate":"2022-04-27T22:00:02","Description":"Irrigation Completed","MessageCode":5},
{"ID":142,"StationNo":19,"RunTime":1800,"ControllerID":4,"ControllerAddress":2,"ProgramNo":5,"ModeID":"AutoProgram","EventDate":"2022-04-27T22:30:02","Description":"Irrigation Completed","MessageCode":5}]

class

public class RawValue
{
    public int ID { get; set; }
    public int StationNo { get; set; }
    public int RunTime { get; set; }
    public int ControllerID { get; set; }
    public int ControllerAddress { get; set; }
    public int ProgramNo { get; set; }
    public string ModeID { get; set; }
    public DateTime EventDate { get; set; }
    public string Description { get; set; }
    public int MessageCode { get; set; }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM