Python通过json.loads dict到JSON:

时间:2021-11-07 03:10:28

I have troubleshooting some code that uses HTTP POST to send data and should return a JSON result whose contents are a dictionary. I am using an XML-RPC wrapper to expose this service. When the wrapper receives the dict information from the http response variable, the dict contents are in a string in this form:

我已经对使用HTTP POST发送数据的一些代码进行了故障排除,并且应该返回其内容为字典的JSON结果。我正在使用XML-RPC包装器来公开此服务。当包装器从http响应变量接收到dict信息时,dict内容以这种形式包含在字符串中:

{'created': datetime.datetime(2010, 12, 31, 19, 13, 8, 379909), 'worker': u'GoogleWorker', 'ready': False, 'request_id': '8f1381853a444a42a37ae5152a3af947', 'owner': u'admin', 'shortname': u'test19'}

I'm trying to convert the string below into a JSON result using the following statement:

我正在尝试使用以下语句将下面的字符串转换为JSON结果:

result = json.loads(response[1])

However, when I try to use json.loads to convert the data to JSON, I get the following error: Fault: <Fault 1: "<type 'exceptions.ValueError'>:Expecting property name: line 1 column 1 (char 1)">

但是,当我尝试使用json.loads将数据转换为JSON时,我收到以下错误:Fault: :Expecting property name:line 1 column 1(char 1 )“>

I manually tried to convert the above string to JSON, but I get the same error. Is the dict malformed in some way? Is it due to unicode? I also tried setting the locale to UTF-8, but that was unsuccessful.

我手动尝试将上面的字符串转换为JSON,但我得到了同样的错误。这个词典在某种程度上是不正确的吗?是unicode吗?我也尝试将语言环境设置为UTF-8,但这不成功。

Any help would be greatly appreciated.

任何帮助将不胜感激。

3 个解决方案

#1


31  

You are trying to use the wrong method. json.loads is for loading JSON to Python. If you want to convert Python to JSON, you need json.dumps.

您正在尝试使用错误的方法。 json.loads用于将JSON加载到Python。如果要将Python转换为JSON,则需要json.dumps。

result = json.dumps(response[1])

#2


5  

That dict is in Python dict literal format, not JSON. You can do:

该dict采用Python dict文字格式,而不是JSON。你可以做:

import ast
result = ast.literal_eval(response[1])

to read in the response in that format. Are you sure that Django hasn't already JSON-decoded the response?

以该格式读取响应。你确定Django还没有JSON解码响应吗?

#3


1  

i have use json on django , i use this :

我在django上使用json,我使用这个:

import simplejson as json
#to encode
final= {'first':first_data,'second':second_data}
json.dumps(final)
#to decode this is the example from python's api 
json.loads('["foo", {"bar":["baz", null, 1.0, 2]}]')

#1


31  

You are trying to use the wrong method. json.loads is for loading JSON to Python. If you want to convert Python to JSON, you need json.dumps.

您正在尝试使用错误的方法。 json.loads用于将JSON加载到Python。如果要将Python转换为JSON,则需要json.dumps。

result = json.dumps(response[1])

#2


5  

That dict is in Python dict literal format, not JSON. You can do:

该dict采用Python dict文字格式,而不是JSON。你可以做:

import ast
result = ast.literal_eval(response[1])

to read in the response in that format. Are you sure that Django hasn't already JSON-decoded the response?

以该格式读取响应。你确定Django还没有JSON解码响应吗?

#3


1  

i have use json on django , i use this :

我在django上使用json,我使用这个:

import simplejson as json
#to encode
final= {'first':first_data,'second':second_data}
json.dumps(final)
#to decode this is the example from python's api 
json.loads('["foo", {"bar":["baz", null, 1.0, 2]}]')