如何解析此JSON文件

时间:2015-11-29 01:18:48

标签: python json

我正在尝试使用Python脚本解析JSON文件:

import json from pprint import pprint

with open("apc.json") as json_file:  
  json_data = json.load(json_file) 
  print (json.dumps(json_data, indent=4))

for item in json_data:    
    y_tube = item.get('bin')
    print(y_tube)
    for each_dict in y_tube:
        print each_dict.get("item")

对于此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"
    } 


 ] 
}

预期产出:

  

Parser应计算来自bin_contents的项目数,并解析bin名称和item来自work_order

示例:

Number of items in Bin-A is 4      #obtained form bin_contents  
  bin_A - oreo_mega_stuf             #obtainedfrom work_order

我收到以下错误:

AttributeError: 'unicode' object has no attribute 'get'

2 个答案:

答案 0 :(得分:0)

json_data是一个字典,所以你的循环写的方式,item是一个键,因此是一个字符串,它可以帮助你解决错误。我没有看到任何bin项,所以不清楚如何解决这个问题。

答案 1 :(得分:0)

你正在重复错误的事情。

for item in json_data:

迭代dict的键(字符串),而不是值,因此在第一个循环中,您可能会获得unicode对象u"bin_contents"。不是与之关联的dict。也许你想要:

for item in json_data.viewvalues():

注意:它仍然赢得get任何有用的内容,因为您的示例没有名为bin的密钥,但这是一个单独的问题。