Python:使用json.loads()读入一个json对象数组

时间:2022-02-06 06:32:09

I have a file data.txt which contains a list of json objects like below:

我有一个文件data.txt,其中包含一个json对象列表,如下所示:

[{"id":"1111","color":["blue"],"length":"120"},{"id":"1112","color":["red"],"length":"130"},{"id":"1112","color":["yellow"],"length":"136"}]

I tried to read it using python json.loads:

我尝试使用python json.loads读取它:

data = json.loads("data.txt")

but then I got the following errors. Did I miss anything here? Thanks a lot!

但后来我遇到了以下错误。我在这里错过了吗?非常感谢!

/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/__init__.pyc in loads(s, encoding, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)
    336             parse_int is None and parse_float is None and
    337             parse_constant is None and object_pairs_hook is None and not kw):
--> 338         return _default_decoder.decode(s)
    339     if cls is None:
    340         cls = JSONDecoder

/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/decoder.pyc in decode(self, s, _w)
    363 
    364         """
--> 365         obj, end = self.raw_decode(s, idx=_w(s, 0).end())
    366         end = _w(s, end).end()
    367         if end != len(s):

/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/decoder.pyc in raw_decode(self, s, idx)
    381             obj, end = self.scan_once(s, idx)
    382         except StopIteration:
--> 383             raise ValueError("No JSON object could be decoded")
    384         return obj, end

ValueError: No JSON object could be decoded

3 个解决方案

#1


7  

You're trying to read the string "data.txt". What you want is to open and read the file.

您正在尝试读取字符串“data.txt”。你想要的是打开并阅读文件。

import json

with open('data.txt', 'r') as data_file:
    json_data = data_file.read()

data = json.loads(json_data)

#2


6  

Try:

尝试:

data = json.load(open("data.txt", 'r'))

json.loads interprets a string as JSON data, while json.load takes a file object and reads it, then interprets it as JSON.

json.loads将字符串解释为JSON数据,而json.load接受一个文件对象并读取它,然后将其解释为JSON。

#3


1  

You need to open the file for reading and read it. To get the behavior you want:

您需要打开文件进行阅读并阅读。获得您想要的行为:

with open('data.txt', 'r') as f:
    data = json.loads(f.read())

That should give you the json structure you want. Using with keeps you from having to close the file explicitly when you're finished.

这应该给你你想要的json结构。使用with可以防止在完成后显式关闭文件。

#1


7  

You're trying to read the string "data.txt". What you want is to open and read the file.

您正在尝试读取字符串“data.txt”。你想要的是打开并阅读文件。

import json

with open('data.txt', 'r') as data_file:
    json_data = data_file.read()

data = json.loads(json_data)

#2


6  

Try:

尝试:

data = json.load(open("data.txt", 'r'))

json.loads interprets a string as JSON data, while json.load takes a file object and reads it, then interprets it as JSON.

json.loads将字符串解释为JSON数据,而json.load接受一个文件对象并读取它,然后将其解释为JSON。

#3


1  

You need to open the file for reading and read it. To get the behavior you want:

您需要打开文件进行阅读并阅读。获得您想要的行为:

with open('data.txt', 'r') as f:
    data = json.loads(f.read())

That should give you the json structure you want. Using with keeps you from having to close the file explicitly when you're finished.

这应该给你你想要的json结构。使用with可以防止在完成后显式关闭文件。