1 回答
TA贡献1875条经验 获得超3个赞
您需要将所选键/值对从json解析为类对象列表,然后再次将其保存到新文件中,例如
private static void ParseData()
{
string json = File.ReadAllText(@"Path to your input json file");
JObject jsonData = JObject.Parse(json);
List<MyCustomClass> output = new List<MyCustomClass>();
foreach (JToken tokens in jsonData["indicator"]["values"])
{
output.Add(new MyCustomClass { datetime = tokens["datetime"].ToString(), value = tokens["value"].ToString() });
}
File.WriteAllText(@"Path to your output json file", JToken.FromObject(output).ToString());
}
你需要一个类对象来解析你的键/值对,
class MyCustomClass
{
public string datetime { get; set; }
public string value { get; set; }
}
输出:
[
{
"datetime": "31-01-2019 05:30:00 AM",
"value": "49.38"
},
{
"datetime": "31-01-2019 06:30:00 AM",
"value": "47.46"
},
{
"datetime": "31-01-2019 07:30:00 AM",
"value": "46.82"
},
{
"datetime": "31-01-2019 08:30:00 AM",
"value": "46.5"
},
{
"datetime": "31-01-2019 09:30:00 AM",
"value": "47.36"
},
{
"datetime": "31-01-2019 10:30:00 AM",
"value": "50.05"
},
{
"datetime": "31-01-2019 11:30:00 AM",
"value": "56.61"
},
{
"datetime": "31-01-2019 12:30:00 PM",
"value": "65.58"
},
{
"datetime": "31-01-2019 01:30:00 PM",
"value": "66.59"
},
{
"datetime": "31-01-2019 02:30:00 PM",
"value": "66.49"
},
{
"datetime": "31-01-2019 03:30:00 PM",
"value": "65.54"
},
{
"datetime": "31-01-2019 04:30:00 PM",
"value": "63.71"
},
{
"datetime": "31-01-2019 05:30:00 PM",
"value": "61.94"
},
{
"datetime": "31-01-2019 06:30:00 PM",
"value": "56.58"
},
{
"datetime": "31-01-2019 07:30:00 PM",
"value": "56.08"
}
]
- 1 回答
- 0 关注
- 88 浏览
添加回答
举报