{
"data":
{
"map":
{
"allowNestedValues": true,
"create": "2012-12-11 15:16:13",
"title": "test201212110004",
"transitions": []
}
},
"msg": "success",
"code": "0"
}
以上是JsonObject
,data
是JsonObject
。
如您所知,如何将其转换为String
"msg":"success"
,我不能直接在data
的值之外添加双引号。
答案 0 :(得分:111)
@hsz我们有内置的方法将JsonObject转换为String。你为什么不用它。
JSONObject json = new JSONObject();
json.toString();
答案 1 :(得分:7)
你可以使用
JsonObject.getString("msg");
答案 2 :(得分:6)
您可以使用:
JSONObject jsonObject = new JSONObject();
jsonObject.toString();
如果您想获得特定值,可以使用:
jsonObject.getString("msg");
或整数值
jsonObject.getInt("codeNum");
答案 3 :(得分:1)
在括号外添加双引号,并将{}
中的双引号替换为\"
所以:"{\"data\":{..... }"
答案 4 :(得分:1)
您可以尝试Gson转换器,以获得json.stringify之类的exacy转换
val jsonString:String = jsonObject.toString()
val gson:Gson = GsonBuilder().setPrettyPrinting().create()
val json:JsonElement = gson.fromJson(jsonString,JsonElement.class)
val jsonInString:String= gson.toJson(json)
println(jsonInString)
答案 5 :(得分:0)
JSONObject metadata = (JSONObject) data.get("map"); //for example
String jsonString = metadata.**toJSONString()**;
答案 6 :(得分:-1)
This should get all the values from the above JsonObject
System.out.println(jsonObj.get("msg"));
System.out.println(jsonObj.get("code"));
JsonObject obj= jsonObj.get("data").getAsJsonObject().get("map").getAsJsonObject();
System.out.println(obj.get("allowNestedValues"));
System.out.println(obj.get("create"));
System.out.println(obj.get("title"));
System.out.println(obj.get("transitions"));
答案 7 :(得分:-2)
您可以使用可靠的库GSON
private static final Type DATA_TYPE_JSON =
new TypeToken<JSONObject>() {}.getType();
JSONObject orderJSON = new JSONObject();
orderJSON.put("noOfLayers", "2");
orderJSON.put("baseMaterial", "mat");
System.out.println("JSON == "+orderJSON.toString());
String dataAsJson = new Gson().toJson(orderJSON, DATA_TYPE_JSON);
System.out.println("Value of dataAsJson == "+dataAsJson.toString());
String data = new Gson().toJson(dataAsJson);
System.out.println("Value of jsonString == "+data.toString());
答案 8 :(得分:-4)
var data= {"data": {"map":{"allowNestedValues": true,"create": "2012-12-11 15:16:13","title": "test201212110004","transitions": []}},"msg": "success","code": "0"}
O / P:
Object {data: Object, msg: "success", code: "0"}
使用JSON.stringify将整个数据转换为字符串,如下所示
var stringData = JSON.stringify(data);
O / P:
"{"data":{"map":{"allowNestedValues":true,"create":"2012-12-11 15:16:13","title":"test201212110004","transitions":[]}},"msg":"success","code":"0"}"
使用JSON.parse将整个字符串对象转换为JSON对象,如下所示
var orgdata = JSON.parse(stringData);
O / P:
Object {data: Object, msg: "success", code: "0"}
答案 9 :(得分:-6)
我认为你需要这个:
假设你有这样的样本
JSON
:
{"ParamOne":"InnerParamOne":"InnerParamOneValue","InnerParamTwo":"InnerParamTwoValue","InnerParamThree":"InnerParamThreeValue","InnerParamFour":"InnerParamFourValue","InnerParamFive":"InnerParamFiveValue"}}
转换为字符串:
String response = {\"ParamOne\":{\"InnerParamOne\":\"InnerParamOneValue\",\"InnerParamTwo\":\"InnerParamTwoValue\",\"InnerParamThree\":\"InnerParamThreeValue\",\"InnerParamFour\":\"InnerParamFourValue\",\"InnerParamFive\":\"InnerParamFiveValue\"}} ;
只需更换&#34;通过\&#34;