Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Python中解析json数据?_Python_Json - Fatal编程技术网

如何在Python中解析json数据?

如何在Python中解析json数据?,python,json,Python,Json,请帮助我用python解析这个json { "IT" : [ { "firstName" : "ajay", "lastName" : "stha", "age" : 24 }, { "firstName" : "Michiel",

请帮助我用python解析这个json

{ "IT" : [   
                            { "firstName" : "ajay",  
                              "lastName"  : "stha",
                              "age"       : 24 },

                            { "firstName" : "Michiel",  
                              "lastName"  : "Og",
                              "age"       : 35 }
                          ],                            
          "sales"       : [ 
                            { "firstName" : "Guru", 
                              "lastName"  : "red",
                              "age"       : 27 },

                            { "firstName" : "Jim",   
                              "lastName"  : "Galley",
                              "age"       : 34 }
                          ] 
        } 
如何在Python中解析此json?请使用以下方法帮助我:


具有嵌套结构的Python字典与JSON数据非常相似, 尽管Python的变量和表达式支持更丰富的结构化选项(任何部分) 下面的表达式可以是Python代码中的任意表达式)

这里显示的最终字典格式是Python代码中的有效文本,几乎是 按原样打印时传递JSON,但JSON模块使翻译正式 -这里,将Python对象转换为JSON序列化字符串表示形式,并将其转换为JSON序列化字符串表示形式 内存中:

>>> import json
>>> json.dumps(rec)
'{"job": ["dev", "mgr"], "name": {"last": "Smith", "first": "Bob"}, "age": 40.5}'
>>> S = json.dumps(rec)
>>> S
'{"job": ["dev", "mgr"], "name": {"last": "Smith", "first": "Bob"}, "age": 40.5}'
>>> O = json.loads(S)
>>> O
{'job': ['dev', 'mgr'], 'name': {'last': 'Smith', 'first': 'Bob'}, 'age': 40.5}
>>> O == rec
True
将Python对象转换为JSON数据字符串和从JSON数据字符串转换Python对象同样简单 在文件中。在存储到文件之前,您的数据只是Python对象;JSON 当模块从 文件:


应该向你提供你需要的所有信息-1因为在发布之前没有进行简单的谷歌搜索。@DanielRoseman你有什么问题吗?我有问题问。你对此有什么问题吗?@Paven gupta谢谢you@backcross如果你觉得它有用,那么请将这个答案标记为正确,这样其他人就可以受益。
import json
jsonResponse = json.loads(data)
jsonDataSales = jsonResponse["sales"]
jsonDataIt = jsonResponse["IT"]
it_first_name_list = []
it_last_name_list = []
it_age_list = []
sales_first_name_list = []
sales_last_name_list = []
sales_age_list = []

for item in jsonDataIt:
    it_first_name_list.append(item.get("firstName"))
    it_last_name_list.append(item.get("lastName"))
    it_age_list.append(item.get("age"))

for item in jsonDataSales:
    sales_first_name_list.append(item.get("firstName"))
    sales_last_name_list.append(item.get("lastName"))
    sales_age_list.append(item.get("age"))
>>> name = dict(first='Bob', last='Smith')
>>> rec = dict(name=name, job=['dev', 'mgr'], age=40.5)
>>> rec
{'job': ['dev', 'mgr'], 'name': {'last': 'Smith', 'first': 'Bob'}, 'age': 40.5}
>>> import json
>>> json.dumps(rec)
'{"job": ["dev", "mgr"], "name": {"last": "Smith", "first": "Bob"}, "age": 40.5}'
>>> S = json.dumps(rec)
>>> S
'{"job": ["dev", "mgr"], "name": {"last": "Smith", "first": "Bob"}, "age": 40.5}'
>>> O = json.loads(S)
>>> O
{'job': ['dev', 'mgr'], 'name': {'last': 'Smith', 'first': 'Bob'}, 'age': 40.5}
>>> O == rec
True
>>> json.dump(rec, fp=open('testjson.txt', 'w'), indent=4)
>>> print(open('testjson.txt').read())
{
"job": [
"dev",
"mgr"
],
"name": {
"last": "Smith",
"first": "Bob"
},
"age": 40.5
}
>>> P = json.load(open('testjson.txt'))
>>> P
{'job': ['dev', 'mgr'], 'name': {'last': 'Smith', 'first': 'Bob'}, 'age': 40.5}