JSON SelectToken返回null

时间:2016-06-10 16:28:44

标签: json parsing

这是我拥有的JSON字符串,我正在尝试解析它以获得“价格,但它总是回来为null,粘贴在下面是我尝试过的,有人可以帮忙吗?

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");

1 个答案:

答案 0 :(得分: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

小提琴演示: https://jsfiddle.net/o6upskoy/