這是JSON字符串:JObject如何讀取數組中的值?
{"d":[{"numberOfRowsAdded":"26723"}]}
string json = DAO.getUploadDataSummary();
JObject uploadData = JObject.Parse(json);
string array = (string)uploadData.SelectToken("d");
如何修改代碼,讀者「的值numberOfRowsAdded?
這是JSON字符串:JObject如何讀取數組中的值?
{"d":[{"numberOfRowsAdded":"26723"}]}
string json = DAO.getUploadDataSummary();
JObject uploadData = JObject.Parse(json);
string array = (string)uploadData.SelectToken("d");
如何修改代碼,讀者「的值numberOfRowsAdded?
你需要轉換爲JArray
:
string json = "{\"d\":[{\"numberOfRowsAdded\":\"26723\"}]}";
JObject parsed = JObject.Parse(json);
JArray array = (JArray) parsed["d"];
Console.WriteLine(array.Count);
JObject uploadData = JObject.Parse(json);
int rowsAdded = Convert.ToInt32((string)uploadData["d"][0]["numberOfRowsAdded"])
不錯,Newtonsoft.Json的開發者。 1+ – IamBatman 2017-10-31 21:28:27
你可以投你JObject
作爲dynamic
對象。
您也可以將您的陣列投射到JArray
對象。
JObject yourObject;
//To access to the properties in "dot" notation use a dynamic object
dynamic obj = yourObject;
//Loop over the array
foreach (dynamic item in obj.d) {
var rows = (int)item.numberOfRowsAdded;
}
這是一個選擇,但強制轉換爲JArray不要求:uploadData [「d」] [0] [「numberOfRowsAdded」] – 2013-03-05 04:29:56
@ JamesNewton王:出於某種原因,我讀到了一個問題,想知道數組中元素的數量*和'JArray'感覺是最簡單的方法。 – 2013-03-05 04:32:07