我在JSON中有以下嵌套的Dictionary。如果我想获取“ id”,“ self”,“ name”,应该如何使用Python程序进行解析。
{
"items": [
{
"id": "12345",
"links": {
"self": "https://www.google.com"
},
"name": "beast",
"type": "Device"
}
],
"links": {
"self": "https://www.google.com"
},
"paging": {
"count": 1,
"limit": 1,
"offset": 0,
"pages": 1
}
}
Run Code Online (Sandbox Code Playgroud) 敬启者,
我的代码错误有什么不对?
>>> import json
>>> array = json.load({"name":"Galen","learning objective":"load json files for data analysis"})
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
array = json.load({"name":"Galen","learning objective":"load json files for data analysis"})
File "C:\Python34\lib\json\__init__.py", line 265, in load
return loads(fp.read(),
AttributeError: 'dict' object has no attribute 'read'
Run Code Online (Sandbox Code Playgroud) 我有一个文件,其结构与python list/dictionaries相同,即
[
{
"key1" : "value1",
"key2" : "value2",
...
},
{
"key1" : "value3",
"key2" : "value4",
...
},
...
]
Run Code Online (Sandbox Code Playgroud)
有一些简单的方法如何读取此文件并将其转换为字典列表?
我想在我的模型中加载以下 json 数据。
{
"99popularity": 79.0,
"director": "William Cottrell",
"genre": [
"Animation",
" Family",
" Fantasy",
" Musical",
" Romance"
],
"imdb_score": 7.9,
"name": "Snow White and the Seven Dwarfs"
},
{
"99popularity": 84.0,
"director": "Stanley Kubrick",
"genre": [
"Adventure",
" Mystery",
" Sci-Fi"
],
"imdb_score": 8.4,
"name": "2001 : A Space Odyssey"
},
Run Code Online (Sandbox Code Playgroud)
我使用 json 数据的引用创建了两个模型
class Genre(models.Model):
name = models.CharField(max_length=30)
class Movie(models.Model):
popularity = models.FloatField(max_length=10)
director = models.CharField(max_length=30)
genre = models.ManyToManyField(Genre)
imdb_score = models.FloatField(max_length=10)
name = models.CharField(max_length=30)
Run Code Online (Sandbox Code Playgroud)
但在 …
我正在读取一个包含逗号分隔的JSON的文件.所以举个例子
{
...JSON
},
{
...JSON
},
{
...JSON
}
Run Code Online (Sandbox Code Playgroud)
我知道他们肯定是用逗号分隔的,但不确定它们是否被换行符分开.这个JSON可能都在一行上.但可以肯定他们被逗号分开了.我还没有收到这些数据.我想知道如何解析每个JSON对象并将其附加到列表中.
伪代码:
def source_parse(source_file):
json_list = []
with open(source_file) as source:
json_source = source.readlines()
# parse json_source
json_list.append(json_obj)
Run Code Online (Sandbox Code Playgroud) 我正在向返回 JSON 文件的 URL 发出请求,
\n\nresponse = requests.get(url)\nresponse = json.loads(response)\nRun Code Online (Sandbox Code Playgroud)\n\n然后,我尝试查看一些数据,
\n\nfor documents in response["docs"]:\n # do something\nRun Code Online (Sandbox Code Playgroud)\n\n现在,我得到的错误是
\n\nTypeError: the JSON object must be str, not \'Response\'\nRun Code Online (Sandbox Code Playgroud)\n\n为了避免这种情况,我尝试过,
\n\nresponse = requests.get(url).json()\nRun Code Online (Sandbox Code Playgroud)\n\n但是,我无法遍历响应,因为我收到错误:
\n\nKeyError: \'docs\'\nRun Code Online (Sandbox Code Playgroud)\n\n我是 Python 新手,并不完全了解获取 JSON 数据并解析它的最佳方法。建议?
\n\n这是接收到的数据的示例,
\n\n\n\n{\'状态\': \'确定\', \'响应\': {\'元\': {\'时间\': 9, \'点击数\': 11, \'偏移量\': 0 }, \'docs\': [{\'type_of_material\': \'新闻\', \'pub_date\': \'2017-01-01T09:12:04+0000\', \'document_type\': \'article\', \'_id\': \'5868c7e995d0e03926078885\', \'lead_paragraph\': \'国家\xe2\x80\x99的领导人自豪地谈到其核武器和弹道导弹计划的进展。\ ',......
\n
我有一个 txt 文件:resultJSON.txt
txt 文件中的数据为 JSON 格式。
{
"term": "dog",
"results": [{
"filename": "1.jpg",
"numberID": "D12"
}, {
"filename": "23.jpg",
"number": "E52"
}]
}
Run Code Online (Sandbox Code Playgroud)
我想将txt读入python并解析JSON。
怎么看txt???
我有Json:
[
{
"name":"Apple",
"price":2,
"have":0,
"max":36
},
{
"name":"Pineapple",
"price":5,
"have":6,
"max":17
}
]
Run Code Online (Sandbox Code Playgroud)
我需要最快的功能,接收名称,并发送价格.例如,print(jsonname("Apple"))是2.
PS请不要发布循环答案,我知道它们.我需要快速的方法和方法的名称