python中字符串和json的转换,如何在python中将字符串数据转换为JSON对象?

Here is the sample string i am receiving from one of the web services,

body=%7B%22type%22%3A%22change%22%2C%22url%22%3A%22http%3A%2F%2Fapi.pachube.com%2Fv2%2Ftriggers%2F4100%22%2C%22environment%22%3A%7B%22feed%22%3A%22http%3A%2F%2Fapi.pachube.com%2Fv2%2Ffeeds%2F36133%22%2C%22title%22%3A%22Current+Cost+Bridge%22%2C%22description%22%3Anull%2C%22id%22%3A36133%7D%2C%22threshold_value%22%3Anull%2C%22timestamp%22%3A%222012-01-05T09%3A27%3A01Z%22%2C%22triggering_datastream%22%3A%7B%22url%22%3A%22http%3A%2F%2Fapi.pachube.com%2Fv2%2Ffeeds%2F36133%2Fdatastreams%2F1%22%2C%22value%22%3A%7B%22value%22%3A%22523%22%2C%22max_value%22%3A1269.0%2C%22min_value%22%3A0.0%7D%2C%22id%22%3A%221%22%2C%22units%22%3A%7B%22symbol%22%3A%22W%22%2C%22type%22%3A%22derivedUnits%22%2C%22label%22%3A%22watts%22%7D%7D%2C%22id%22%3A4100%7D

Here is the code,

class Feeds():

def GET(self):

print "Get request is accepted."

return render.index(None)

def POST(self):

print "Post request is accepted."

print (web.data())

Now when that web-service posts the above given data, how will i convert it to readable format? Then, i need to convert it to JSON object and use further. So, how will i convert it?

When i try this code,

json_data = json.loads(web.data())

print json_data['body']

return render.index(json_data['body'])

It gives me an error,

enter code Traceback (most recent call last):

File "/usr/local/lib/python2.6/dist-packages/web/application.py", line 237, in process

return self.handle()

File "/usr/local/lib/python2.6/dist-packages/web/application.py", line 228, in handle

return self._delegate(fn, self.fvars, args)

File "/usr/local/lib/python2.6/dist-packages/web/application.py", line 409, in _delegate

return handle_class(cls)

File "/usr/local/lib/python2.6/dist-packages/web/application.py", line 385, in handle_class

return tocall(*args)

File "/home/ubuntu/pachubeConsumer/src/controllers/feeds.py", line 17, in POST

json_data = json.loads(web.data())

File "/usr/lib/python2.6/json/__init__.py", line 307, in loads

return _default_decoder.decode(s)

File "/usr/lib/python2.6/json/decoder.py", line 319, in decode

obj, end = self.raw_decode(s, idx=_w(s, 0).end())

File "/usr/lib/python2.6/json/decoder.py", line 338, in raw_decode

raise ValueError("No JSON object could be decoded")

ValueError: No JSON object could be decoded here

Where am i going wrong ??

Thanks in advance.

解决方案

First you need to unquote the body

import urllib

body="%7B%22type%22%3A%22change%22%2C%22url%22%3A%22http%3A%2F%2Fapi.pachube.com%2Fv2%2Ftriggers%2F4100%22%2C%22environment%22%3A%7B%22feed%22%3A%22http%3A%2F%2Fapi.pachube.com%2Fv2%2Ffeeds%2F36133%22%2C%22title%22%3A%22Current+Cost+Bridge%22%2C%22description%22%3Anull%2C%22id%22%3A36133%7D%2C%22threshold_value%22%3Anull%2C%22timestamp%22%3A%222012-01-05T09%3A27%3A01Z%22%2C%22triggering_datastream%22%3A%7B%22url%22%3A%22http%3A%2F%2Fapi.pachube.com%2Fv2%2Ffeeds%2F36133%2Fdatastreams%2F1%22%2C%22value%22%3A%7B%22value%22%3A%22523%22%2C%22max_value%22%3A1269.0%2C%22min_value%22%3A0.0%7D%2C%22id%22%3A%221%22%2C%22units%22%3A%7B%22symbol%22%3A%22W%22%2C%22type%22%3A%22derivedUnits%22%2C%22label%22%3A%22watts%22%7D%7D%2C%22id%22%3A4100%7D"

unquoted = urllib.unquote(body)

Then just load the JSON like normal

import json

pythonDict = json.loads(unquoted)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值