本文介绍了将字符串保留在JSONObject中作为包含JSONObject的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
目前我有这样的东西:
import net.sf.json.JSONObject
JSONObject jsonObject = new JSONObject();
String thing = "[231,232,234]"
jsonObject.put("mything",thing)
JSONObject params = new JSONObject()
params.put("props",jsonObject)
问题是当我在jsonObject中查看mything
时,它是一个正确的String
.但是,当我在params
中查看mything
时,它是一个JSONArray
.如何保持字符串?
The problem is when I look at mything
in jsonObject it is a String
which is correct. However, when I look at mything
in params
it is a JSONArray
. How can I keep it a string?
推荐答案
由于您使用Groovy标记了问题,其本机JsonBuilder
似乎可以正常工作:
Since you tagged the question with Groovy, its native JsonBuilder
seems to work as expected:
import groovy.json.*
a = new JsonBuilder(mything: "[1, 2, 3]")
assert a.toString() == '{"mything":"[1, 2, 3]"}'
b = new JsonBuilder(params: a.getContent())
assert b.toString() == '{"params":{"mything":"[1, 2, 3]"}}'
这篇关于将字符串保留在JSONObject中作为包含JSONObject的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!