I want to generate a JSON string in the following structure using the Jackson API (JsonFactory, JsonGenerator). How can i do this?
Expected:
{ "api": { "Salutaion": "Mr", "name": "X" }, "additional": { "Hello", "World" } }
Actual:
{ "api": "{ \"Salutaion\": \"Mr\", \"name\": \"X\" }", "additional": "{ \"Hello\", \"World\" }" }
The values โโof the api and optional attributes will be available to me as String. Should I use writeObjectField (as follows)?
jGenerator.writeObjectField("api", apiString);
After creating the jGenerator object, how to get the final constructed representation of a JSON Object string?
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); JsonGenerator jGenerator = jfactory.createJsonGenerator(outputStream); jGenerator.writeStartObject(); jGenerator.writeObjectField("api", apiString); jGenerator.writeObjectField("additional", additionalString); jGenerator.writeEndObject(); jGenerator.close(); outputStream.close(); outputStream.toString()
OutputStream.toString () gives a json string, but double quotes (") in apiString get the escape character prefix \
Is it correct?
source share