Python json.load返回字符串而不是字典

Python json.load返回字符串而不是字典,python,json,Python,Json,我接收一个json文件,只将必要的键及其值复制到一个新的json文件中。我得到了一个错误“TypeError:字符串索引必须是整数”,这与我将值复制到myDict的位置有关。据我所知,json.load返回的是字符串而不是字典。我验证了json文件,它具有有效的json格式。我正在使用Python 2.7.12。我到处都找遍了,没有找到一个能回答我具体问题的答案。非常感谢你能给我的任何帮助 import os import sys import json def stripSpec(inp,

我接收一个json文件,只将必要的键及其值复制到一个新的json文件中。我得到了一个错误“TypeError:字符串索引必须是整数”,这与我将值复制到myDict的位置有关。据我所知,json.load返回的是字符串而不是字典。我验证了json文件,它具有有效的json格式。我正在使用Python 2.7.12。我到处都找遍了,没有找到一个能回答我具体问题的答案。非常感谢你能给我的任何帮助

import os
import sys
import json

def stripSpec(inp, outp):
    #Load json file as python dictionary
    obj  = json.load(open(inp, "r"))

    result=[]

    #Go through JSON and save necessary keys and values
    for i in obj:
        myDict = {}
        myDict["id"]=i.get('id').get('value')
        myDict["data"]["BaselineExposure"]=i.get('data').get('BaselineExposure').get('value')
        myDict["data"]["ColorMatrix2"]=i.get('data').get('ColorMatrix2').get('value')
        result.append(myDict)

    # Output the updated file with pretty JSON
    open(outp, "w").write(json.dumps(result, sort_keys=True, indent=4, ensure_ascii=False, separators=(',', ':')))
    return

#Save input and output paths as variables
inp = sys.argv[1]
outp = sys.argv[2]

#Call function
stripSpec(inp, outp)
这里是json的一个示例。它已经大幅减少,但基本上每个相机型号都有更多的条目

[
{ "id": "Canon EOS 100D",
 "data":[{
  "SourceFile": "./Canon 100D/canon_eos_100d_11.dng",
  "ExifToolVersion": 10.07,
  "Directory": "./Canon 100D",
  "FileSize": "18 MB",
  "FileModifyDate": "2016:05:02 23:03:14-07:00",
  "FileAccessDate": "2016:05:03 01:45:03-07:00",
  "FileInodeChangeDate": "2016:05:02 23:03:14-07:00",
  "FilePermissions": "rw-r--r--",
  "ColorMatrix2": "0.6602 -0.0841 -0.0939 -0.4472 1.2458 0.2247 -0.0975 0.2039 0.6148",
  "CameraCalibration1": "1.0648 0 0 0 1 0 0 0 0.9881",
  "CameraCalibration2": "1.0648 0 0 0 1 0 0 0 0.9881",
  "AnalogBalance": "1 1 1",
  "AsShotNeutral": "0.512769 1 0.584809",
  "BaselineExposure": -0.25,
  "RedBalance": 1.950195
  }]
},
在json存根
中,“数据”
键包含列表。在代码中,您将其称为字典:
i.get('data').get('BaselineExposure')

相反,您应该遍历
“数据”
。 例如:

data = i.get('data')
for d in data:
    print(d.get('BaselineExposure'))
因此,基本上要小心嵌套项


还有,为什么要使用
i.get('id').get('value')
。相反,
i.get('id')
应该足够了,额外的
.get('value')
应该会引发
AttributeError,不是吗?

概述:我假设json存储为字典。我使用json.dumps将字典转换为json字符串。我pip安装json2xml json2xml来转换json字符串,以便将其转换为xml。然后,我将xml加载到dom树中进行搜索。我使用getElementsByTagName在xml树中搜索节点并显示值。我的方法对程序员更友好

from json2xml import json2xml
from json2xml.utils import readfromurl, readfromstring, readfromjson
from xml.dom.minidom import parse, parseString

dict={
  "id": "Canon EOS 100D",
  "data": [{
    "SourceFile": "./Canon 100D/canon_eos_100d_11.dng",
    "ExifToolVersion": 10.07,
    "Directory": "./Canon 100D",
    "FileSize": "18 MB",
    "FileModifyDate": "2016:05:02 23:03:14-07:00",
    "FileAccessDate": "2016:05:03 01:45:03-07:00",
    "FileInodeChangeDate": "2016:05:02 23:03:14-07:00",
    "FilePermissions": "rw-r--r--",
    "ColorMatrix2": "0.6602 -0.0841 -0.0939 -0.4472 1.2458 0.2247 -0.0975 0.2039 0.6148",
    "CameraCalibration1": "1.0648 0 0 0 1 0 0 0 0.9881",
    "CameraCalibration2": "1.0648 0 0 0 1 0 0 0 0.9881",
    "AnalogBalance": "1 1 1",
    "AsShotNeutral": "0.512769 1 0.584809",
    "BaselineExposure": -0.25,
    "RedBalance": 1.950195
    }]
 }

 #convert dictionary to a string
 json_data=json.dumps(dict,indent=4)
 data=readfromstring(json_data)
 xml=json2xml.Json2xml(data).to_xml()

 dom=parseString(xml)

 element=dom.getElementsByTagName('BaselineExposure') 
 print(element[0].firstChild.nodeValue)

否则手动搜索:def printDictionary(dict,leafNode):在dict.keys()中搜索j:如果j==leafNode:print(“找到”+j+':',dict[j]),否则:在dict.keys()中为key打印(j+':',dict[j])leafNode=“BaselineExposure”:如果str(type(dict[key]),则打印(type(dict[key]):listItems=dictlistItems中的项的[key]:if str(type(item))=“printDictionary(item,leafNode)else:print(key+”:”,dict[key])