回答此问题可获得 20 贡献值,回答如果被采纳可获得 50 分。
<p>我试图从JSON提要中读取单个值。以下是feed数据的示例:</p>
<pre><code>{
"sendtoken": "token1",
"bytes_transferred": 0,
"num_retries": 0,
"timestamp": 1414395374,
"queue_time": 975,
"message": "internalerror",
"id": "mailerX",
"m0": {
"binding_group": "domain.com",
"recipient_domain": "hotmail.com",
"recipient_local": "destination",
"sender_domain": "domain.com",
"binding": "mail.domain.com",
"message_id": "C1/34-54876-D36FA645",
"api_credential": "creds",
"sender_local": "localstring"
},
"rejecting_ip": "145.5.5.5",
"type": "alpha",
"message_stage": 3
}
{
"sendtoken": "token2",
"bytes_transferred": 0,
"num_retries": 0,
"timestamp": 1414397568,
"queue_time": 538,
"message": "internal error,
"id": "mailerX",
"m0": {
"binding_group": "domain.com",
"recipient_domain": "hotmail.com",
"recipient_local": "destination",
"sender_domain": "domain.com",
"binding": "mail.domain.com",
"message_id": "C1/34-54876-D36FA645",
"api_credential": "creds",
"sender_local": "localstring"
},
"rejecting_ip": "145.5.5.5",
"type": "alpha",
"message_stage": 3
}
</code></pre>
<p>我不能共享实际的URL,但是上面是大约150个结果中的前两个,如果我运行</p>
<pre><code>print results
</code></pre>
<p>在</p>
<pre><code>json.loads()
</code></pre>
<p>行。</p>
<p>我的代码:</p>
<pre><code>import urllib2
import json
results = urllib2.urlopen(url).read()
jsondata = json.loads(results)
for row in jsondata:
print row['sendtoken']
print row['recipient_domain']
</code></pre>
<p>我想输出如下</p>
<pre><code>token1
hotmail.com
</code></pre>
<p>对于每个条目。</p>
<p>我得到这个错误:</p>
<pre><code>ValueError: Extra data: line 2 column 1 - line 133 column 1 (char 583 - 77680)
</code></pre>
<p>我不是Python专家,这是我第一次使用JSON。我花了很多时间在google和Stack Overflow上,但找不到适合我特定数据格式的解决方案。</p>