版本:Python 2.7.10。
我有以下内容:
{"type":"quote","symbol":"SPY","bid":266.52,"bidsz":5,"bidexch":"P","biddate":"1513040398000","ask":266.55,"asksz":100,"askexch":"P","askdate":"1513040399000"}
{"type":"trade","symbol":"SPY","exch":"P","price":"266.31","size":"0","cvol":"83077533","date":"1513040400000","last":"266.31"}
{"type":"summary","symbol":"SPY","open":"265.58","high":"266.38","low":"265.4793","prevClose":"265.51","close":"266.31"}
<type 'unicode'>
b
输出
new = simplejson.loads(r.text)
print(new)
我想得到输出“SPY”。
我添加了以下内容:
Traceback (most recent call last):
File "example.py", line 63, in <module>
new = simplejson.loads(r.text)
File "/Library/Python/2.7/site-packages/simplejson/__init__.py",
line 518, in loads
return _default_decoder.decode(s)
File "/Library/Python/2.7/site-packages/simplejson/decoder.py",
line 373, in decode
raise JSONDecodeError("Extra data", s, end, len(s))
simplejson.errors.JSONDecodeError: Extra data: line 1 column 160 -
line 1 column 407 (char 159 - 406)
现在,我得到以下内容:
r = requests.post(url = API_ENDPOINT, headers = headers, data =
data).json()
我改变了:
Traceback (most recent call last):
File "example.py", line 51, in <module>
r = requests.post(url = API_ENDPOINT, headers = headers, data =
data).json()
File "/Library/Python/2.7/site-packages/requests/models.py", line
884, in json
self.content.decode(encoding), **kwargs
File "/Library/Python/2.7/site-packages/simplejson/__init__.py",
line 518, in loads
return _default_decoder.decode(s)
File "/Library/Python/2.7/site-packages/simplejson/decoder.py",
line 373, in decode
raise JSONDecodeError("Extra data", s, end, len(s))
simplejson.errors.JSONDecodeError: Extra data: line 1 column 160 -
line 1 column 407 (char 159 - 406)
现在,我明白了:
new = json.dumps(r.text)
print(new)
print(type(new))
我补充说:
"{\"type\":\"quote\",\"symbol\":\"SPY\",\"bid\":266.52,\"bidsz\":5,\"bidexch\":\"P\",\"biddate\":\"1513040398000\",\"ask\":266.55,\"asksz\":100,\"askexch\":\"P\",\"askdate\":\"1513040399000\"}
{\"type\":\"trade\",\"symbol\":\"SPY\",\"exch\":\"P\",\"price\":\"266.31\",\"size\":\"0\",\"cvol\":\"83077533\",\"date\":\"1513040400000\",\"last\":\"266.31\"}
{\"type\":\"summary\",\"symbol\":\"SPY\",\"open\":\"265.58\",\"high\":\"266.38\",\"low\":\"265.4793\",\"prevClose\":\"265.51\",\"close\":\"266.31\"}"
<type 'str'>
现在,输出是:
for line in r.text.splitlines():
d = json.loads(line)
如果我这样做:
Traceback (most recent call last):
File "example.py", line 54, in <module>
d = json.loads(line)
File "/System/Library/Frameworks/Python.
framework/Versions/2.7/lib/python2.7/json/__init__.py", line 338, in
loads
return _default_decoder.decode(s)
File "/System/Library/Frameworks/Python.framework/
Versions/2.7/lib/python2.7/json/decoder.py", line 369, in decode
raise ValueError(errmsg("Extra data", s, end, len(s)))
ValueError: Extra data: line 1 column 158 - line 1 column 404
(char 157 - 403)
我明白了:
for line in r.text.splitlines():
print(line)
print("\n\n")
所以我试过了:
{"type":"quote","symbol":"SPY","bid":267.18,"bidsz":1,"bidexch":"P","biddate":"1513213200000","ask":267.22,"asksz":3,"askexch":"P","askdate":"1513213200000"}
{"type":"quote","symbol":"SPY","bid":267.18,"bidsz":1,"bidexch":"P","biddate":"1513213200000","ask":267.22,"asksz":3,"askexch":"P","askdate":"1513213200000"}
我得到了:
%b
所以尽管应该有两行,但它将所有内容都解释为一行。
如何将r.text转换为字典?
答案 0 :(得分:1)
您应该使用此.json()
:
public int intOfIpV4(String ip) {
int result = 0;
byte[] bytes = IPAddressUtil.textToNumericFormatV4(ip);
if (bytes == null) {
return result;
}
for (byte b : bytes) {
result = result << 8 | (b & 0xFF);
}
return result;
}
答案 1 :(得分:1)
我相信您收到的是JSON Lines格式。响应的每一行都是一个json字符串。我模拟了回应:
import json
class r: pass
r.text = u'''\
{"type":"quote","symbol":"SPY","bid":266.52,"bidsz":5,"bidexch":"P","biddate":"1513040398000","ask":266.55,"asksz":100,"askexch":"P","askdate":"1513040399000"}
{"type":"trade","symbol":"SPY","exch":"P","price":"266.31","size":"0","cvol":"83077533","date":"1513040400000","last":"266.31"}
{"type":"summary","symbol":"SPY","open":"265.58","high":"266.38","low":"265.4793","prevClose":"265.51","close":"266.31"}
'''
print(r.text)
print(type(r.text))
print(r.text[2])
# Parse JSON a line at a time:
for line in r.text.splitlines():
d = json.loads(line)
print d['symbol']
输出:
{"type":"quote","symbol":"SPY","bid":266.52,"bidsz":5,"bidexch":"P","biddate":"1513040398000","ask":266.55,"asksz":100,"askexch":"P","askdate":"1513040399000"}
{"type":"trade","symbol":"SPY","exch":"P","price":"266.31","size":"0","cvol":"83077533","date":"1513040400000","last":"266.31"}
{"type":"summary","symbol":"SPY","open":"265.58","high":"266.38","low":"265.4793","prevClose":"265.51","close":"266.31"}
<type 'unicode'>
t
SPY
SPY
SPY