简体   繁体   English

使用换行符将json转换为字符串并在python中转义

[英]convert json to string with newline and escape in python

how to convert using python below payloads:如何在有效载荷下使用 python 进行转换:

{
    "abc": {
        "i": "1212",
        "j": "add"
    }
}

to

"{\n\n    \"abc\": {\n        \"i\": \"1212\",\n        \"j\": \"add\"\n    }\n}"

You can use the optional indent parameter of json.dump and json.dumps to add newlines and indent to the generated string.您可以使用json.dumpjson.dumps的可选indent参数为生成的字符串添加换行符和缩进。

>>> import json
>>> payload = {
...     "assessmentstatus": {
...         "id": "D37002079003",
...         "value": "In-Progress"
...     }
... }
...
>>> json.dumps(payload)
'{"assessmentstatus": {"id": "D37002079003", "value": "In-Progress"}}'
>>> json.dumps(payload, indent=0)
'{\n"assessmentstatus": {\n"id": "D37002079003",\n"value": "In-Progress"\n}\n}'
>>> json.dumps(payload, indent=2)
'{\n  "assessmentstatus": {\n    "id": "D37002079003",\n    "value": "In-Progress"\n  }\n}'

With properly displayed whitespace:正确显示空格:

>>> print(json.dumps(payload, indent=2))
{
  "assessmentstatus": {
    "id": "D37002079003",
    "value": "In-Progress"
  }
}

It seems you actually want the string including the enclosing " and with all the " within the string escaped.看来您实际上希望字符串包括封闭的"并且字符串中的所有"都被转义了。 This is surprisingly tricky using Python's repr , as it always tries to use either ' or " as the outer quotes so that the quotes do not have to be escaped.这是令人惊讶的棘手使用Python的repr ,因为它总是试图要么使用'"与外引号,这样引号不必转义。

What seems to work, though, is to just json.dumps the JSON string again :不过,似乎可行的是再次json.dumps JSON 字符串:

>>> json.dumps(json.dumps(payload, indent=2))
'"{\\n  \\"assessmentstatus\\": {\\n    \\"id\\": \\"D37002079003\\",\\n    \\"value\\": \\"In-Progress\\"\\n  }\\n}"'
>>> print(json.dumps(json.dumps(payload, indent=2)))
"{\n  \"assessmentstatus\": {\n    \"id\": \"D37002079003\",\n    \"value\": \"In-Progress\"\n  }\n}"

Im assuming it has newlines since its in a file我假设它有换行符,因为它在一个文件中

with open('file.json') as f: data = json.load(f)

Then do what you want with the data然后用数据做你想做的事

You can save data to a file in JSON format like this您可以像这样以 JSON 格式将数据保存到文件中

import json

data  = {
    "assessmentstatus": {
        "id": "D37002079003",
        "value": "In-Progress"
    }
}
with open("file.txt", "w") as file:
   json.dump(data, file, indent=4)

To retrieve this data you can do要检索此数据,您可以执行以下操作

import json

with open("file.txt") as file:
    data = json.load(file)

print(data)

JSON is a way to store store data in a string format and be able to parse that data to convert it to actual data. JSON 是一种以字符串格式存储数据并能够解析该数据以将其转换为实际数据的方法。

So trying to convert "{\\n\\n "assessmentstatus": {\\n "id": "D37002079003",\\n "value": "In-Progress"\\n }\\n}" on your own is the wrong way to go at this, since JSON is great at doing this.因此,尝试"{\\n\\n "assessmentstatus": {\\n "id": "D37002079003",\\n "value": "In-Progress"\\n }\\n}"转换"{\\n\\n "assessmentstatus": {\\n "id": "D37002079003",\\n "value": "In-Progress"\\n }\\n}"是错误的方法继续这样做,因为 JSON 非常擅长这样做。

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

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