【问题标题】:Get value from JSON object in c#从 C# 中的 JSON 对象获取值
【发布时间】:2020-07-04 03:46:16
【问题描述】:

我从一个 url 获得 JSON 响应,并使用 jObj2 中的以下代码捕获它

dynamic jObj2 = JsonConvert.DeserializeObject(resultCheck.Content.ReadAsStringAsync().Result);
Response.Write("<p>"+jObj2+"<p>");

输出是:

{
    "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"
            }
        }
    } }

我如何访问单个值

caridcoderef...

我尝试了下面的代码,但它给了我错误

var jsonData2 = (JObject)JsonConvert.DeserializeObject(resultCheck.Content.ReadAsStringAsync().Result);
    
Response.Write("<br> jsonData2['order']['description'].ToString() " + jsonData2["order"]["url"].ToString())

错误信息

  Object reference not set to an instance of an object. Description: An
 unhandled exception occurred during the execution of the current web
 request. Please review the stack trace for more information about the
 error and where it originated in the code.
 
 Exception Details: System.NullReferenceException: Object reference not
 set to an instance of an object.
 
 Source Error:
 
 
 Line 65:                 Response.Write("<br>"); Line 66:             
 Response.Write("<br>"); Line 67:                 Response.Write("<br>
 jsonData2['order']['description'].ToString() " +
 jsonData2["order"]["description"].ToString()); Line 68:  Line 69:            
 }

如何从 json 对象中访问单个值

【问题讨论】:

  • 我解决了这个问题,因为我在以下语句 jsonData2["order"]["description"].ToString() 中引用了错误的值
  • 为什么-1,你解释一下

标签: c# json webforms


【解决方案1】:

没有尝试过,但我认为,这是最相对的答案

https://stackoverflow.com/a/4749755/5458824

【讨论】:

  • 我解决了这个问题,因为我错误地指向了错误的对象
【解决方案2】:

这可能有助于获得所需的输出

JToken token = JObject.Parse(JsonConvert.DeserializeObject(resultCheck.Content.ReadAsStringAsync().Result));
Response.Write("<br> token.SelectToken("description") " + token.SelectToken("description"))

【讨论】:

  • 我怎样才能找到code,它在两个地方status&gt;code | transaction&gt;code
  • 我建议您使用Json.Net 从这个 JSON 结构中获取值。
  • 我没有使用 json.net using Newtonsoft.Json;
猜你喜欢
  • 2018-11-15
  • 1970-01-01
  • 1970-01-01
  • 2016-01-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多