我在json文本中使用彩色html标签时遇到问题
/etc/apt/source.list
json编辑器无法将此代码识别为有效的JSON,如何将带有颜色的html文本添加到json中。
答案 0 :(得分:1)
因为该字符串用双引号"
引起来,所以您必须在字符串内部转义双引号(通过在反斜杠{{1}前面加上前缀),或使用单引号\
,也是可接受的JSON语法。
转义的双引号:
'
单引号:
{
"name": "john",
"dsc": "<p>hello <span style=\"color: #ff0000;\">people</span>, <strong>how</strong> are you</p>"
}
答案 1 :(得分:0)
我使用https://github.com/fangyidong/json-simple,它正确地转义了这一行
样品
JSONObject jsonObject = new JSONObject();
jsonObject.put("name", "john");
jsonObject.put("dsc", "<p>hello <span style=\"color: #ff0000;\">people</span>, <strong>how</strong> are you</p>");
System.err.println(jsonObject.toJSONString());
System.err.println(jsonObject.get("name").toString());
System.err.println(jsonObject.get("dsc").toString());
结果
{
"name":"john",
"dsc":"<p>hello <span style=\"color: #ff0000;\">people<\/span>, <strong>how<\/strong> are you<\/p>"
}
john
<p>hello <span style="color: #ff0000;">people</span>, <strong>how</strong> are you</p>