我从网址获取jSon响应,我正在jObj2
dynamic jObj2 = JsonConvert.DeserializeObject(resultCheck.Content.ReadAsStringAsync().Result);
Response.Write("<p>"+jObj2+"<p>");
OUTPUT
{
"method":"check",
"trace":"1234/12345/123456f5",
"order":{
"ref":"2910730E3E90D40F59BC9B738E71EF7AD9DF679C93D34EAEADA6775216F12C13",
"cartid":"1723873570174343",
"test":1,
"amount":"30.00",
"currency":"USD",
"description":"Descsdaff",
"status":{
"code":3,
"text":"Paid"
},
"transaction":{
"ref":"019463997038",
"type":"sale",
"class":"ECom",
"status":"A",
"code":"919514",
"message":"Authorised"
},
"card":{
"type":"Visa Credit",
"last4":"0002",
"expiry":{
"month":3,
"year":2017
}
},
"customer":{
"email":"email@email.com",
"name":{
"forenames":"James",
"surname":"Senior"
},
"address":{
"line1":"Denvour",
"city":"Denvour",
"country":"US"
}
}
} }
如何访问
的各个值 carid
code
ref
...
我尝试了下面的代码,但它给了我错误
var jsonData2 = (JObject)JsonConvert.DeserializeObject(resultCheck.Content.ReadAsStringAsync().Result);
Response.Write("<br> jsonData2['order']['description'].ToString() " + jsonData2["order"]["url"].ToString())
错误消息
对象引用未设置为对象的实例。描述:一个 在执行当前Web期间发生了未处理的异常 请求。请查看堆栈跟踪以获取有关的更多信息 错误以及它在代码中的起源。
异常详细信息:System.NullReferenceException:不是对象引用 设置为对象的实例。
来源错误:
第65行:Response.Write(&#34;
&#34;);第66行:
回复于(&#34;
&#34);第67行:Response.Write(&#34;
jsonData2 [&#39; order&#39;] [&#39; description&#39;]。ToString()&#34; + jsonData2 [&#34;为了&#34;] [&#34;描述&#34]。的ToString());第68行:第69行:
}
如何从json对象
访问各个值答案 0 :(得分:1)
Haven没试过这个,但我想,这是最相对的答案
答案 1 :(得分:1)
这可能有助于获得所需的输出
JToken token = JObject.Parse(JsonConvert.DeserializeObject(resultCheck.Content.ReadAsStringAsync().Result));
Response.Write("<br> token.SelectToken("description") " + token.SelectToken("description"))