【问题标题】:How can we parse the JSON file such that it gets read by an object automatically?我们如何解析 JSON 文件以使其被对象自动读取?
【发布时间】:2016-01-17 18:00:40
【问题描述】:

这是我的 JSON 文件,虽然我可以以 data["bin_contents"]["bin_A"][0] 等形式访问内容,但如何在无需指定“bin_A”、“bin_B”等的情况下自动读取输入?下面给出的是 JSON 文件:

{
  "bin_contents":
  {
    "bin_A":
    [
      "oreo_mega_stuf","champion_copper_plus_spark_plug","expo_dry_erase_board_eraser","kong_duck_dog_toy"
    ],
    "bin_B":
    [
      "genuine_joe_plastic_stir_sticks"
    ],
    "bin_C":
    [
      "munchkin_white_hot_duck_bath_toy"
    ],
    "bin_D":
    [
      "crayola_64_ct"
    ],
    "bin_E":
    [
      "mommys_helper_outlet_plugs","sharpie_accent_tank_style_highlighters","kong_air_dog_squeakair_tennis_ball"
    ],
    "bin_F":
    [
      "stanley_66_052"
    ],
    "bin_G":
    [
      "safety_works_safety_glasses","dr_browns_bottle_brush","laugh_out_loud_joke_book"
    ],
    "bin_H":
    [
      "cheezit_big_original","paper_mate_12_count_mirado_black_warrior"
    ],
    "bin_I":
    [
      "feline_greenies_dental_treats","elmers_washable_no_run_school_glue"
    ],
    "bin_J":
    [
      "mead_index_cards","rolodex_jumbo_pencil_cup","mead_index_cards","first_years_take_and_toss_straw_cup"
    ],
    "bin_K":
    [
      "highland_6539_self_stick_notes","mark_twain_huckleberry_finn"
    ],
    "bin_L":
    [
      "kyjen_squeakin_eggs_plush_puppies","kong_sitting_frog_dog_toy"
    ]
  },

  "work_order":
  [
    {
      "bin": "bin_A",
      "item": "oreo_mega_stuf"
    },
    {
      "bin": "bin_B",
      "item": "genuine_joe_plastic_stir_sticks"
    },
    {
      "bin": "bin_C",
      "item": "munchkin_white_hot_duck_bath_toy"
    },
    {
      "bin": "bin_D",
      "item": "crayola_64_ct"
    },
    {
      "bin": "bin_E",
      "item": "mommys_helper_outlet_plugs"
    },
    {
      "bin": "bin_F",
      "item": "stanley_66_052"
    },
    {
      "bin": "bin_G",
      "item": "safety_works_safety_glasses"
    },
    {
      "bin": "bin_H",
      "item": "cheezit_big_original"
    },
    {
      "bin": "bin_I",
      "item": "feline_greenies_dental_treats"
    },
    {
      "bin": "bin_J",
      "item": "mead_index_cards"
    },
    {
      "bin": "bin_K",
      "item": "highland_6539_self_stick_notes"
    },
    {
      "bin": "bin_L",
      "item": "kyjen_squeakin_eggs_plush_puppies"
    }
  ]
}

【问题讨论】:

  • 您想要的是将 json 反序列化为一个对象。我可以向你建议的是,研究在 python 中反序列化 json。

标签: python json


【解决方案1】:

你会迭代它的键/值

代替

data["bin_contents"]["bin_A"][0]

使用

for key, value in data.items():
    print key  # bin_contents
    print value  #  {"bin_A": ["oreo_mega_stuf","champion_copper_plus_spark_plug" ...]}
    for key1, value1 in value.items():
        print key1  # "bin_A
        print value1:  # ["oreo_mega_stuf","champion_copper_plus_spark_plug" ...]

【讨论】:

    【解决方案2】:

    只需循环遍历数据结构与对键进行硬编码:

    for bin, value in data['contents'].items():
        print(bin, value[0])
    

    【讨论】:

      猜你喜欢
      • 2017-04-29
      • 1970-01-01
      • 2021-08-07
      • 1970-01-01
      • 2020-07-21
      • 2017-09-25
      • 1970-01-01
      • 2011-10-15
      • 2013-04-26
      相关资源
      最近更新 更多