我有一個如下所示的 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 和轉義字符。
uj5u.com熱心網友回復:
基本上,您已經獲得了包含 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
用于“嵌入式”物件。如果您也想對它們進行建模,您將擁有:
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);
當您反序列化兩次時,不應該有任何“額外”轉義。
uj5u.com熱心網友回復:
試試這個
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}]
班級
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; }
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/473574.html