遍历json并显示属性值

时间:2017-08-14 12:06:16

标签: javascript json

我正在尝试输出以下json的部分内容:

如何从pint输出值beer.name.568.name

{
    "beer": {
        "name": "Beer or cider",
        "sizes": {
            "568": {
                "name": "Pint",
                "size": 0.568,
                "id": "pint",
                "max": 10,
                "icon": "beer_pint"
            },
            "440": {
                "name": "Can",
                "size": 0.44,
                "id": "can",
                "max": 10,
                "icon": "beer_can"
            },
            "330": {
                "name": "Bottle",
                "size": "0.33",
                "id": "bottle",
                "max": "10",
                "icon": "beer_bottle_330"
            },
            "275": {
                "name": "Small bottle",
                "size": 0.275,
                "id": "smallBottle",
                "max": 10,
                "icon": "beer_bottle_275"
            }
        },
        "strength": [4, 4.5, 5, 6, 7, 8, 9]
    },
    "wine": {
        "name": "Wine or champagne",
        "sizes": {
            "125": {
                "name": "Small glass",
                "size": 0.125,
                "id": "small",
                "max": 10,
                "icon": "wine_small_glass"
            },
            "175": {
                "name": "Standard glass",
                "size": 0.175,
                "id": "std",
                "max": 10,
                "icon": "wine_standard_glass"
            },
            "250": {
                "name": "Large glass",
                "size": 0.25,
                "id": "large",
                "max": 10,
                "icon": "wine_large_glass"
            },
            "1000": {
                "name": "Bottle",
                "size": 1,
                "id": "bottle",
                "max": 10,
                "icon": "wine_bottle"
            }
        },
        "strength": [9, 10, 10.5, 11, 11.5, 12, 13, 14, 15, 16, 17]
    },
    "spirits": {
        "name": "Spirits or shots",
        "sizes": {
            "25": {
                "name": "Single",
                "size": 0.025,
                "id": "single",
                "max": 10,
                "icon": "spirit_single"
            },
            "35": {
                "name": "Large single",
                "size": 0.035,
                "id": "lgSingle",
                "max": 10,
                "icon": "spirit_large_single"
            },
            "50": {
                "name": "Double",
                "size": 0.05,
                "id": "double",
                "max": 10,
                "icon": "spirit_double"
            },
            "70": {
                "name": "Large double",
                "size": 0.07,
                "id": "lgDouble",
                "max": 10,
                "icon": "spirit_large_double"
            },
            "700": {
                "name": "Bottle",
                "size": 0.7,
                "id": "bottle700",
                "max": 3,
                "icon": "spirit_bottles"
            },
            "1000": {
                "name": "Bottle",
                "size": 1,
                "id": "bottle",
                "max": 3,
                "icon": "spirit_bottles"
            }
        },
        "strength": [37, 40]
    },
    "alcopop": {
        "name": "Alcopop",
        "sizes": {
            "275": {
                "name": "Small bottle",
                "size": 0.275,
                "id": "small",
                "max": 10,
                "icon": "alcopops_small_bottle"
            },
            "330": {
                "name": "Standard bottle",
                "size": 0.33,
                "id": "std",
                "max": 10,
                "icon": "alcopops_standard_bottle"
            },
            "750": {
                "name": "Large bottle",
                "size": 0.75,
                "id": "large",
                "max": 10,
                "icon": "alcopops_large_bottle"
            }
        },
        "strength": [5, 5.5]
    }
}

2 个答案:

答案 0 :(得分:0)

显然这种方法可能有许多缺点,因为循环在某些情况下会变得无限,但是,如果你知道你的结构是“稳定的”,这可以模拟一些动态json模式中的搜索,当你知道一个键和想要获得房产价值。

var data = {
  "beer": {
    "name": "Beer or cider",
    "sizes": {
      "568": {
        "name": "Pint",
        "size": 0.568,
        "id": "pint",
        "max": 10,
        "icon": "beer_pint"
      },
      "440": {
        "name": "Can",
        "size": 0.44,
        "id": "can",
        "max": 10,
        "icon": "beer_can"
      },
      "330": {
        "name": "Bottle",
        "size": "0.33",
        "id": "bottle",
        "max": "10",
        "icon": "beer_bottle_330"
      },
      "275": {
        "name": "Small bottle",
        "size": 0.275,
        "id": "smallBottle",
        "max": 10,
        "icon": "beer_bottle_275"
      }
    },
    "strength": [4, 4.5, 5, 6, 7, 8, 9]
  },
  "wine": {
    "name": "Wine or champagne",
    "sizes": {
      "125": {
        "name": "Small glass",
        "size": 0.125,
        "id": "small",
        "max": 10,
        "icon": "wine_small_glass"
      },
      "175": {
        "name": "Standard glass",
        "size": 0.175,
        "id": "std",
        "max": 10,
        "icon": "wine_standard_glass"
      },
      "250": {
        "name": "Large glass",
        "size": 0.25,
        "id": "large",
        "max": 10,
        "icon": "wine_large_glass"
      },
      "1000": {
        "name": "Bottle",
        "size": 1,
        "id": "bottle",
        "max": 10,
        "icon": "wine_bottle"
      }
    },
    "strength": [9, 10, 10.5, 11, 11.5, 12, 13, 14, 15, 16, 17]
  },
  "spirits": {
    "name": "Spirits or shots",
    "sizes": {
      "25": {
        "name": "Single",
        "size": 0.025,
        "id": "single",
        "max": 10,
        "icon": "spirit_single"
      },
      "35": {
        "name": "Large single",
        "size": 0.035,
        "id": "lgSingle",
        "max": 10,
        "icon": "spirit_large_single"
      },
      "50": {
        "name": "Double",
        "size": 0.05,
        "id": "double",
        "max": 10,
        "icon": "spirit_double"
      },
      "70": {
        "name": "Large double",
        "size": 0.07,
        "id": "lgDouble",
        "max": 10,
        "icon": "spirit_large_double"
      },
      "700": {
        "name": "Bottle",
        "size": 0.7,
        "id": "bottle700",
        "max": 3,
        "icon": "spirit_bottles"
      },
      "1000": {
        "name": "Bottle",
        "size": 1,
        "id": "bottle",
        "max": 3,
        "icon": "spirit_bottles"
      }
    },
    "strength": [37, 40]
  },
  "alcopop": {
    "name": "Alcopop",
    "sizes": {
      "275": {
        "name": "Small bottle",
        "size": 0.275,
        "id": "small",
        "max": 10,
        "icon": "alcopops_small_bottle"
      },
      "330": {
        "name": "Standard bottle",
        "size": 0.33,
        "id": "std",
        "max": 10,
        "icon": "alcopops_standard_bottle"
      },
      "750": {
        "name": "Large bottle",
        "size": 0.75,
        "id": "large",
        "max": 10,
        "icon": "alcopops_large_bottle"
      }
    },
    "strength": [5, 5.5]
  }
};

function findPropertyValueForKey(prev, key, property, data) {
  if (typeof data != "object") return;
  var keys = Object.keys(data);

  for (var i = 0; i < keys.length; i++) {
    if (keys[i] == key) {
      prev.push(data[key][property]);
    } else {
      findPropertyValueForKey(prev, key, property, data[keys[i]]);
    }
  }
};

var results = [];

findPropertyValueForKey(results, "568", "name", data);

console.log(results);

答案 1 :(得分:0)

&#13;
&#13;
var beer = {
  "sizes": {
    "568": {
      "name": "Pint"
    }
  }
}

console.log(beer.sizes[568].name);          // "Pint"

console.log(beer['sizes'][568]['name']);    // "Pint"
&#13;
&#13;
&#13;