2016-06-10 2 views
0

Это строка JSON у меня есть, и я пытаюсь разобрать его, чтобы получить «цену, но она всегда возвращается нуль, наклеенные ниже того, что я попробовал, может кто-то пожалуйста, помогите?JSON SelectToken возвращение нулевой

Строка JSON

{ 
    "data": { 
     "id": "529665606157020735", 
     "metadata": { 
      "product": { 
       "offer": { 
        "price": "$72", 
        "in_stock": true 
       } 
      }, 
      "link": { 
       "locale": "en", 
       "title": "Missguided Lace Trim Crepe Bardot Romper | Nordstrom", 
       "site_name": "Nordstrom", 
       "description": "Free shipping and returns on Missguided Lace Trim Crepe Bardot Romper at Nordstrom.com. Playful lace insets trim the off-the-shoulder bodice of this breezy flutter-sleeve romper, while sweet scallops detail the hem to complete the look.", 
       "favicon": "https://s-media-cache-ak0.pinimg.com/favicons/4f607a214d617b57b7e45330f8f9135392b7b569f688523cc64bff6e.png?944d8af95205d57ea43fd0128d28a846" 
      } 
     } 
    } 
    } 

Синтаксический Сценарий:

jsonstring = await response.Content.ReadAsStringAsync(); 
JObject jresults = JObject.Parse(jsonstring); 
string price = (string)jresults.SelectToken("metadata.product.offer.price"); 

ответ

0

Попробуйте это:

JSON:

var obj = { 
    "data": { 
     "id": "529665606157020735", 
     "metadata": { 
      "product": { 
       "offer": { 
        "price": "$72", 
        "in_stock": true 
       } 
      }, 
      "link": { 
       "locale": "en", 
       "title": "Missguided Lace Trim Crepe Bardot Romper | Nordstrom", 
       "site_name": "Nordstrom", 
       "description": "Free shipping and returns on Missguided Lace Trim Crepe Bardot Romper at Nordstrom.com. Playful lace insets trim the off-the-shoulder bodice of this breezy flutter-sleeve romper, while sweet scallops detail the hem to complete the look.", 
       "favicon": "https://s-media-cache-ak0.pinimg.com/favicons/4f607a214d617b57b7e45330f8f9135392b7b569f688523cc64bff6e.png?944d8af95205d57ea43fd0128d28a846" 
      } 
     } 
    } 
}; 

сценарий:

var result = obj.data.metadata.product.offer.price; 
console.log(result); 

Выход:

enter image description here

Fiddle demo:https://jsfiddle.net/o6upskoy/

+0

Thanks @RohitJindal! изменение пути к строке price = (строка) jresults.SelectToken ("data.metadata.product.offer.price"); исправлена ​​проблема. – Melody

+0

@Melody upvote и отметьте это как правильный ответ, поэтому он будет полезен и для других. –

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