C#使用Newtonsoft.json

时间:2016-09-08 11:24:05

标签: c# json

我想从Json响应中只提取一个属性,并在根元素中提取它。我尝试了几次尝试,但无法取得成功。请指教。

[
  {
    "discountAmount": 0,
    "discountPercentage": 0,
    "originalPrice": null,
    "manufacturerProductCode": "9H.JA677.25E",
    "bulletPoints": [
      "Full HD (1920x1080)",
      "Kuvakoko 26\"-300\"",
      "Suuri valovoima, 4000 Lm"
    ],
    "href": "/fi/product/16592/dxrdm/BenQ-SH915-Full-HD-DLP-asennettava-projektori",
    "name": "BenQ SH915 Full HD DLP ‐asennettava projektori",
    "images": [
      {
        "host": "cdn-b.verkkokauppa.com",
        "path": "/images/14/2_195368-2500x1488.jpeg"
      },
      {
        "host": "cdn-a.verkkokauppa.com",
        "path": "/images/36/2_195368-3000x1790.jpeg"
      },
      {
        "host": "cdn-d.verkkokauppa.com",
        "path": "/images/51/2_195368-3000x1153.jpeg"
      },
      {
        "host": "cdn-b.verkkokauppa.com",
        "path": "/images/81/2_195368-3000x1425.jpeg"
      },
      {
        "host": "cdn-a.verkkokauppa.com",
        "path": "/images/96/2_195368-3000x1449.jpeg"
      },
      {
        "host": "cdn-d.verkkokauppa.com",
        "path": "/images/11/2_195368-3000x2200.jpeg"
      },
      {
        "host": "cdn-c.verkkokauppa.com",
        "path": "/images/26/2_195368-3000x1161.jpeg"
      }
    ],

这是Json响应,我需要(manufacturerProductCode)来自此响应。

1 个答案:

答案 0 :(得分:2)

Dynamic object可以轻松解决此问题。或者你可以试试Linq到Json。

dynamic obj = JObject.Parse(json);
Console.WriteLine((string)obj.manufacturerProductCode);