json形如
{ "object":{ "name":"cwr" }, "others":"123" }
要獲取name的值,則需要構造兩個JObject來獲取,如下:
JObject json = JObject.Parse(jsonresult);
string name = ((JObject)json["object"])["name"].ToString();
json形如
{ "object":{ "name": [ { "firstname" : "cwr", "lastname" : "cwr" } ] }, "others":"123" }
獲取firstname的值如下:
JObject json = JObject.Parse(jsonresult); string firstname = ((JObject)((JObject)json["object"])["name"][0])[firstname].ToString();
昨天調試程序一直報"未將對象引用到實例",報錯代碼是 fpmccontent.Text = json["words_result"]["InvoiceType"].ToString() ?? "";
我一直以為是json["words_result"]["InvoiceType"].ToString()為null,但實際上是json["words_result"]["InvoiceType"]為null,是調用ToString()方法的時候報錯了,對自己無語。