获取JSON子元素值

时间:2018-09-16 12:09:21

标签: c# .net json

我正在尝试从此JSON获取spuCode

{
  "msg": "success",
  "state": 0,
  "data": {
    "result": {
      "spuCode": "541426110605",
      "productName": "纯黑色斜纹面料铅笔裤百搭大码小脚裤弹力打底裤休闲裤子女",
      "productTitle": null,
      "spuImgs": [
        "https://cbu01.alicdn.com/img/ibank/2016/276/468/3618864672_1742354982.jpg",
        "https://cbu01.alicdn.com/img/ibank/2016/793/372/3617273397_1742354982.jpg",
        "https://cbu01.alicdn.com/img/ibank/2016/726/552/3617255627_1742354982.jpg",
        "https://cbu01.alicdn.com/img/ibank/2017/521/101/4624101125_1742354982.jpg",
        "https://cbu01.alicdn.com/img/ibank/2017/070/749/4580947070_1742354982.jpg"
      ],
      "upAndDown": 1,
      "updateTime": 1537096913958,
      "platform": "ALIBABA",
      "skus": [
        {
          "skuCode": "3488434133172",
          "sellPrice": 3900,
          "sellableNum": 905,
          "productProps": [
            {
              "propId": 7590793702270582000,
              "valueId": 5453504708925905000,
              "propName": "颜色",
              "valueName": "纯黑色(相同面料)"
            },
            {
              "propId": 9000005669393888000,
              "valueId": 6217370164147047000,
              "propName": "尺码",
              "valueName": "XXL"
            }
          ],

如您所见,有一个父母data和一个孩子result,所以我添加了以下代码:

string sp = "";
var obj = Newtonsoft.Json.Linq.JObject.Parse(responseFromServer);
foreach (JObject child in obj["data"]["result"].OfType<JObject>())
{
    sp = child["spuCode"].ToString();
    MessageBox.Show(sp);
}

但是它永远不会触发MessageBox.Show我在这里错过了什么?

1 个答案:

答案 0 :(得分:3)

normals issue不是数组,因此不需要consonants = ["qwrtpsdfghjklzxcvbnm"] summer_word = "icecream" new_word = "" vowels = 'aeiou' for consonants in summer_word: if consonants.lower() not in vowels and type(consonants) != int: new_word += consonants answer = new_word

尝试使用result这样的方法:

foreach