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

Posted

技术标签:

【中文标题】如何在python中将字符串数据转换为JSON对象?【英文标题】:How to convert a string data to a JSON object in python? 【发布时间】:2012-02-03 03:36:48 【问题描述】:

这是我从其中一个 Web 服务接收到的示例字符串,

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

这是代码,

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())

现在,当该网络服务发布上述给定数据时,我将如何将其转换为可读格式?然后,我需要将其转换为 JSON 对象并进一步使用。那么,我将如何转换它?

当我尝试这段代码时,

        json_data = json.loads(web.data())
        print json_data['body']
        return render.index(json_data['body'])

它给了我一个错误,

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

我哪里出错了??

提前致谢。

【问题讨论】:

【参考方案1】:

首先你需要取消引用正文

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)

然后像往常一样加载 JSON

import json
pythonDict = json.loads(unquoted)

【讨论】:

是的!这可能刚刚解决了我所有的 JSON 到 Python 的问题【参考方案2】:

您需要先使用urllib.unquote() 取消转义查询字符串

import urllib

unescaped = urllib.unquote(web.data())

那么您可以使用json.loads 将其转换为json。

json_data = json.loads(unescaped)

【讨论】:

【参考方案3】:

Python 3 urllib unquote 移至解析:

from urllib import parse
parse.unquote(web.data())

【讨论】:

以上是关于如何在python中将字符串数据转换为JSON对象?的主要内容,如果未能解决你的问题,请参考以下文章

如何在 Python 中将类对象转换为 JSON

如何在 Spring Boot 中将输入字符串转换为 json 字符串或 json 对象

如何在android中将json对象从json数组转换为字符串数组

如何在 Python 中将 JSON 字符串转换为字典?

如何在 PHP 中将字符串转换为 JSON 对象

如何在 Kotlin Android 中将字符串转换为 JSON 对象 [重复]