如何对嵌入在JSON中的JSON进行编码 [英] How to encode JSON embedded within JSON

查看:118
本文介绍了如何对嵌入在JSON中的JSON进行编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个JSON字符串,并且其中一个字段是文本字段.该文本字段可以包含用户在UI中输入的文本,如果他们输入的文本是JSON文本(也许为了说明一些编码),我需要对其文本进行编码,以便在发送的实际JSON结构中不会将其解释为JSON.到服务器.

I have a JSON string and one of the fields is a text field. This text field can contain text that a user enters in the UI and if the text they enter is JSON text, perhaps to illustrate some coding, I need to encode their text so that it does not get interpreted as JSON within the actual JSON structure sent to the server.

当服务器接收到JSON结构并对其进行解码时,我需要确保嵌入式JSON被解码为文本,最终在用户界面中看起来像JSON.

When the JSON structure is received by the server and gets decoded, I need to make sure the embedded JSON gets decoded as text, which ends up looking like JSON in the UI.

实际上,您如何转义嵌入式JSON字符串?

In effect, how do you escape an embedded JSON string?

推荐答案

我正在做类似的事情,只是用XML而不是JSON:在接收格式错误或其他不可处理的数据时,服务器返回一个包含一些信息的错误结构.和原始数据.为了防止客户端再次解析损坏的数据,该数据在服务器上被 base64编码.

I'm doing something similar, just with XML instead of JSON: On receiving malformed or otherwise non-processable data the server returns an error-structure containing some information and the original data. To prevent the client from parsing the corrupt data again it's base64 encoded on the server.

因此,而不是发送

{
  title : "My sample code",
  payload : "{ \"foo\" : \"bar\" }"
}

考虑发送

{
  title : "My encoded sample code",
  payload : "eyAiZm9vIiA6ICJiYXIiIH0="
}

这篇关于如何对嵌入在JSON中的JSON进行编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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