我有一个包含以下数据的json字符串:
[
{
"RecordId": "1800",
"CrewId": "M9703",
"Rank": "3RD OFFICER",
"VesselCode": "Honourable Henry Jackman",
"MovementType": "ON LEAVE",
"StartDate": "2009-01-13",
"EndDate": "2012-06-30",
"DueDate": "2009-01-02",
"StartLoc": "",
"EndLoc": "",
"CompletionReason": "",
"Remarks": "",
"Last_Edited_Date": "2013-10-18"
},
{
"RecordId": "4880",
"CrewId": "M9703",
"Rank": "2ND OFFICER",
"VesselCode": "Ore Salobo",
"MovementType": "ON LEAVE",
"StartDate": "2013-02-10",
"EndDate": "2013-08-26",
"DueDate": "2012-12-28",
"StartLoc": "",
"EndLoc": "",
"CompletionReason": "NORMAL DUTIES",
"Remarks": "",
"Last_Edited_Date": "2013-10-18"
}
]
我想在控制台中将其显示为键值对。
我试过将它显示为键和值对:
string json = JsonConvert.SerializeObject(res);
var Dejson = JsonConvert.DeserializeObject<dynamic>(json);
foreach (var pair in Dejson)
{
Console.WriteLine("{0}: {1}", pair.Key, pair.Value);
}
但它只在控制台中显示:
。
答案 0 :(得分:1)
// assuming res is list of objects and RecordId is string not integer/long
string json = JsonConvert.SerializeObject(res);
Dictionary<string, res> Dejson = JsonConvert.DeserializeObject<dynamic>(json).ToDictionary(k => k.RecordId, v => v);
foreach ((KeyValuePair<string, res> pair in Dejson)
{
Console.WriteLine("{0}: {1}", pair.Key, pair.Value);
}
答案 1 :(得分:0)
JArray obj = JArray.Parse(json);
Response.Write(obj[0]["RecordId"]);
使用上面的代码,您可以从JSon对象中获取所需的值。