2017-02-07 2 views
-1

Я получаю ответ JSON из URL и я захватывая его с помощью следующего кода в jObj2Получить значение из объекта JSON в C#

 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 protected]", 
      "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("
"); Line 66:
Response.Write("
"); Line 67: Response.Write("
jsonData2['order']['description'].ToString() " + jsonData2["order"]["description"].ToString()); Line 68: Line 69:
}

Как я могу получить доступ отдельные значения из объекта JSon

+0

Я решил эту проблему, так как я имел в виду неправильное значение в следующем заявление 'jsonData2 [«порядок»] [«описание»] ToString()' – Learning

+0

Why. - 1, ca вы объясните – Learning

ответ

1

Не пробовал это, но я думаю, это наиболее относительный ответ

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

+0

Я решил проблему, поскольку по ошибке я указывал на неправильный объект – Learning

1

Это может помочь, чтобы получить желаемый результат

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

Как я могу обратиться к «коду», который находится в двух местах «status> code' | 'transaction> code' – Learning

+0

Я бы предложил вам использовать [' Json.Net'] (http://stackoverflow.com/a/40416621/3796048) для получения значений из этой структуры JSON. –

+0

Я не использую json.net 'using Newtonsoft.Json;' – Learning

Смежные вопросы