sau*_*abh -1 .net c# parsing json json.net
我有一个 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"
}
]
}
Run Code Online (Sandbox Code Playgroud)
如何反序列化此 JSON 并创建一个仅包含 RawValue 内部值的列表,其中每个有效负载将充当列表中的单个元素。另外,如何从列表中的每个元素中删除不需要的 \r\n 和转义字符。
基本上,您已经获得了包含 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; }
}
Run Code Online (Sandbox Code Playgroud)
然后:
public class Root
{
public List<Value> Values { get; set; }
}
public class Value
{
// Add other properties if you need them
public string RawValue { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
假设您很乐意将JArray
/JObject
用于“嵌入”对象。如果您也想对它们进行建模,您将拥有:
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);
Run Code Online (Sandbox Code Playgroud)
...然后进行反序列化:
public class Station
{
[JsonProperty("ID")]
public int Id { get; set; }
public int StationNo { get; set; }
// etc
}
Run Code Online (Sandbox Code Playgroud)
当您反序列化两次时,不应该有任何“额外”转义。