简体   繁体   English

如何将字符串响应转换为有效的 JSON?

[英]How can I convert a string response to a valid JSON?

Hi I am trying to convert a string response to a valid json list of objects in python.嗨,我正在尝试将字符串响应转换为 python 中的对象的有效 json 列表。

value= "{ActionSuccess=True; AdditionalActionsBitMask=0},{ActionSuccess=True; AdditionalActionsBitMask=0}"

I tried json.loads but I got the error json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 1 column 3 (char 2)我试过json.loads但我收到错误json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 1 column 3 (char 2)

I tried to convert using regex but it only return the first object.我尝试使用正则表达式进行转换,但它只返回第一个 object。

regex = re.compile(r"\b(\w+)=([^=]*)(?=\s\w+=\s*|$)")
diction = dict(regex.findall(value))

The result is {'ActionSuccess': 'True;', 'AdditionalActionsBitMask': '0}'}结果是{'ActionSuccess': 'True;', 'AdditionalActionsBitMask': '0}'}

To convert a Python string to JSON, use the json.要将 Python 字符串转换为 JSON,请使用 json。 loads() function.负载()function。 The loads() method accepts a valid json string and returns a dictionary to access all elements. load() 方法接受一个有效的 json 字符串并返回一个字典来访问所有元素。

I would turn your string into valid JSON following way:我会将您的字符串转换为有效的 JSON 以下方式:

import json
import re
value= "{ActionSuccess=True; AdditionalActionsBitMask=0},{ActionSuccess=True; AdditionalActionsBitMask=0}"
valuejson = '[' + re.sub(r'\b','"',value).replace(';',',').replace('=',':') + ']'
data = json.loads(valuejson)
print(data)

output output

[{'ActionSuccess': 'True', 'AdditionalActionsBitMask': '0'}, {'ActionSuccess': 'True', 'AdditionalActionsBitMask': '0'}]

I place " where word boundary are (note that \b is zero-length so nothing will be removed) then change ; to , and = to : , finally encose whole string into [ and ] to get list of dict s. Beware that this solution is made specifically to your shown example, so please test it carefully for your other use cases.我将"放置在单词边界的位置(注意\b的长度为零,因此不会删除任何内容)然后将;更改为,并将=更改为: ,最后将整个字符串包含在[]中以获取dictlist 。请注意,这个解决方案是专门针对您显示的示例制定的,因此请仔细测试您的其他用例。

If you have access to documentation of what is giving you said response please consult it regarding what format is used in response.如果您可以访问有关给您所说响应的文档,请查阅它以了解响应中使用的格式。 If it is not proprietary one you might be able to find converter for it on PyPI .如果它不是专有的,您可以在PyPI上找到它的转换器。

Because you input the wrong JSON string, {'ActionSuccess': 'True', 'AdditionalActionsBitMask': '0'} and {'ActionSuccess': 'True', 'AdditionalActionsBitMask': '0'} should have key for them, try this:因为您输入了错误的 JSON 字符串, {'ActionSuccess': 'True', 'AdditionalActionsBitMask': '0'}{'ActionSuccess': 'True', 'AdditionalActionsBitMask': '0'}应该有他们的关键,试试这个:

import json
data = '{\"block1\":{\"ActionSuccess\":\"True\", \"AdditionalActionsBitMask\":\"0\"},\"block2\":{\"ActionSuccess\":\"True\", \"AdditionalActionsBitMask\":\"0\"}}'
print(json.loads(data))

output: output:

{'block1': {'ActionSuccess': 'True', 'AdditionalActionsBitMask': '0'}, 'block2': {'ActionSuccess': 'True', 'AdditionalActionsBitMask': '0'}}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM