中间包含“\\\”的JSON数据 [英] JSON data containing `\\\` in between

查看:253
本文介绍了中间包含“\\\”的JSON数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个写作Flask应用程序的python监听服务器。该服务器正在侦听来自远程源的POST消息。远程源正在发布JSON文档。我收到的示例JSON文档如下所示。

I have a python listening server written as a Flask application.This server is listening to POST messages from a remote source.The remote source is posting JSON documents. A sample JSON document that I recieved is shown below.

{

    "Timestamp": "1432241553492",
    "data": "[{\"EventName\":\"Time\",\"Category\":\"Experience\",\"Severity\":\"warn\",\"Subject\":\"Time\",\"Message\":\"details:{\\\"Message\\\":\\\"https://xxxx.xxxxx.com/ (1882 ms : ATime: 5 ms, BTime: 1108 ms, CTime: 769 ms), \\\",\\\"Time\\\":\\\"Fri May 22 2015 08:52:33 GMT+1200 (NZST)\\\",\\\"MobileDevice\\\":\\\"Not Mobile\\\",\\\"User\\\":\\\"user.name\\\",\\\"CPUs\\\":8,\\\"Language\\\":\\\"en-GB\\\",\\\"isEvent\\\":\\\"true\\\",\"Stamp\":\"1432241553492\"}]",
    "msgType": "0",
    "tid": "1"
}

这个文件应该是一个合适的JSON文件。但我在字段之间得到 \\\ ,如上所示。我想知道是否有问题我的侦听服务器中的Http OPTIONS设置或者数据类型。
如果有人能帮我解决这个问题会很棒。

This file is supposed to be a proper JSON file.But I get \\\ in between the fields as shown above.I am wondering if something is wrong with the setting of Http OPTIONS in my listening server or the data type perhaps. It would be great if someone can help me to figure it out.

推荐答案

乍一看你似乎有一个拙劣的JSON文件;那里有一个迷路的序列,它打破了格式。

At first glance you appear to have a botched JSON file; there is a stray ," sequence in there that breaks the format.

如果你删除了条目,它可能会很好是你打破了格式;如果你的实际字符串在 http://jsonlint.com 上有效,那么你就是这么做的。

If you removed entries, it may well be that you broke the format; if your actual string validates on http://jsonlint.com then you did just that.

反斜杠是JSON中有效的转义序列。您的数据包含其他JSON字符串,而这些字符串又包含更多编码的JSON数据。您可以递归地解码那些:

Backslashes are valid escape sequences in JSON. You have data that contains other JSON strings, which in turn contain more encoded JSON data. You can recursively decode those:

>>> import json
>>> print json.dumps({'object': 'deeply nested'})
{"object": "deeply nested"}
>>> print json.dumps({'wrapper': json.dumps({'object': 'deeply nested'})})
{"wrapper": "{\"object\": \"deeply nested\"}"}
>>> print json.dumps({'outermost': json.dumps({'wrapper': json.dumps({'object': 'deeply nested'})})})
{"outermost": "{\"wrapper\": \"{\\\"object\\\": \\\"deeply nested\\\"}\"}"}

请注意,随着包装级别的增加,反斜杠也会增加。首先,嵌入的引用转义为 \,然后转义反斜杠并将引号转换为 \\\等。

Note that as the level of wrapping increases, so do the backslashes. First the embedded " quotes get escaped to \", followed by escaping of the backslash and the quote to \\\", etc.

您可能想要修复产生此嵌套的代码。 t编码单个对象,然后将它们存储在其他对象中。

You probably want to fix the code that produces this nesting. Don't encode individual objects, then store them in something else.

不要这样做:

event['details'] = json.dumps(event_detail_data)
message['data'] = json.dumps(event)
json_to_send = json.dumps(message)

这会创建一个嵌套结构。只编码最终对象:

That'd create a nested structure. Only encode the final object:

event['details'] = event_detail_data
message['data'] = event
json_to_send = json.dumps(message)

这篇关于中间包含“\\\”的JSON数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆