Convert JsonObject to String
JavaStringJava Problem Overview
{
"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
.
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.
Java Solutions
Solution 1 - Java
There is an inbuilt method to convert a JSONObject to a String. Why don't you use that:
JSONObject json = new JSONObject();
json.toString();
Solution 2 - Java
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");
Solution 3 - Java
you can use
JsonObject.getString("msg");
Solution 4 - Java
You can try Gson convertor, to get the exact conversion like 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)
Solution 5 - Java
JsonObject seems to be JSON-P API. If this is true, I would use JsonWritter to write JsonValue into 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();
Solution 6 - Java
Add a double quotes outside the brackets and replace double quotes inside the {}
with \"
So: "{\"data\":{..... }"
Solution 7 - Java
JSONObject metadata = (JSONObject) data.get("map"); //for example
String jsonString = metadata.**toJSONString()**;
Solution 8 - Java
just use 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);
Solution 9 - Java
Use This :
JSONObject json = new JSONObject();
JSONObject.valueToString(json.toString());
Solution 10 - Java
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"));
Solution 11 - Java
You can use reliable library 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());
Solution 12 - Java
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
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
var orgdata = JSON.parse(stringData);
o/p:
Object {data: Object, msg: "success", code: "0"}
Solution 13 - Java
I think you need this :
> Suppose you have Sample JSON
like this :
{"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 "