[英]Convert JsonObject to String
{
"data":
{
"map":
{
"allowNestedValues": true,
"create": "2012-12-11 15:16:13",
"title": "test201212110004",
"transitions": []
}
},
"msg": "success",
"code": "0"
}
Above is a JsonObject
, the data
is a JsonObject
.上面是一个JsonObject
, data
是一个JsonObject
。
How to convert it to a String
like "msg":"success"
as you know, i can't directly add a double quotes outside data
's value.如您所知,如何将其转换为像"msg":"success"
这样的String
,我不能直接在data
的值之外添加双引号。
There is an inbuilt method to convert a JSONObject to a String.有一个内置方法可以将 JSONObject 转换为字符串。 Why don't you use that:你为什么不使用它:
JSONObject json = new JSONObject();
json.toString();
You can use:您可以使用:
JSONObject jsonObject = new JSONObject();
jsonObject.toString();
And if you want to get a specific value, you can use:如果你想获得一个特定的值,你可以使用:
jsonObject.getString("msg");
or Integer value或整数值
jsonObject.getInt("codeNum");
您可以使用
JsonObject.getString("msg");
You can try Gson convertor, to get the exact conversion like json.stringify您可以尝试 Gson 转换器,以获得像 json.stringify 这样的精确转换
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)
Use This:用这个:
JSONObject json = new JSONObject();
JSONObject.valueToString(json.toString());
Add a double quotes outside the brackets and replace double quotes inside the {}
with \\"
在括号外添加双引号并将{}
内的双引号替换为\\"
So: "{\\"data\\":{..... }"
所以: "{\\"data\\":{..... }"
JsonObject seems to be JSON-P API. JsonObject 似乎是 JSON-P API。 If this is true, I would use JsonWritter to write JsonValue into StringWriter:如果这是真的,我将使用 JsonWritter 将 JsonValue 写入 StringWriter:
JsonObjectBuilder pokemonBuilder = Json.createObjectBuilder();
pokemonBuilder.add("name", "Pikachu");
pokemonBuilder.add("type", "electric");
pokemonBuilder.add("cp", 827);
pokemonBuilder.add("evolve", true);
JsonObject pokemon = pokemonBuilder.build();
StringWriter sw = new StringWriter(128);
try (JsonWriter jw = Json.createWriter(sw)) {
jw.write(pokemon);
}
String pokemonStr = sw.toString();
JSONObject metadata = (JSONObject) data.get("map"); //for example
String jsonString = metadata.**toJSONString()**;
just use ObjectMapper只需使用 ObjectMapper
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS,false);
//here more config opts...
Car car = new Car("yellow", "renault");
objectMapper.writeValue(new File("target/car.json"), car);
String carAsString = objectMapper.writeValueAsString(car);
JSONObject data = (JSONObject) data.get("map");
//for example
String jsonString = data.toJSONString();
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"));
You can use reliable library GSON您可以使用可靠的库 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());
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"}
Use JSON.stringify to convert entire data into string like below使用 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"}"
Use JSON.parse to convert entire string object into JSON Object like below使用 JSON.parse 将整个字符串对象转换为 JSON 对象,如下所示
var orgdata = JSON.parse(stringData);
o/p:开/关:
Object {data: Object, msg: "success", code: "0"}
I think you need this :我认为你需要这个:
Suppose you have Sample
JSON
like this :假设您有这样的示例JSON
:
{"ParamOne":"InnerParamOne":"InnerParamOneValue","InnerParamTwo":"InnerParamTwoValue","InnerParamThree":"InnerParamThreeValue","InnerParamFour":"InnerParamFourValue","InnerParamFive":"InnerParamFiveValue"}}
Converted to String :转换为字符串:
String response = {\"ParamOne\":{\"InnerParamOne\":\"InnerParamOneValue\",\"InnerParamTwo\":\"InnerParamTwoValue\",\"InnerParamThree\":\"InnerParamThreeValue\",\"InnerParamFour\":\"InnerParamFourValue\",\"InnerParamFive\":\"InnerParamFiveValue\"}} ;
Just replace " by \\"只需将“替换为\\”
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.