4

I am using json developing android sdk. I found writing json string is annoying, take a look:

post_my_server("{\"cmd\":\"new_comment\"}");

I need to manually escape quotes, is there any clean way to do this?

3 Answers 3

7

You can use single quotes "{'cmd':'new_comment'}".

Alternatively, there is free code at http://json.org/java/ for implementing JSON at an object level. It's free as in very permissive, I am no lawyer, but it would seem that the only stipulation is that the software you include it in is good, and not evil.

Sign up to request clarification or add additional context in comments.

Comments

2
To create json string use jackson core jars. The code snippet to generate a sample json string ,

JsonFactory jFactory = new JsonFactory();
StringWriter writer = new StringWriter();
/*** write to string ***/
JsonGenerator jsonGenerator = jFactory.createJsonGenerator(writer);
jsonGenerator.writeStartObject();
jsonGenerator.writeStringField("company", [Value]);
jsonGenerator.writeNumberField("salary", [value]); 
jsonGenerator.writeEndObject();
jsonGenerator.close();
String jsonString = writer.toString();

Comments

1

I use the jackson json parsers and serializers , they are completely self contained, and allow for reading, writing of any java object to and from json.

Assume you have a file called "user.json" which contains a big json in it....

private static void convert(){ Map<String,Object> userData = mapper.readValue(new File("user.json"), Map.class); }

1 Comment

The Jackson JSON libraries are great.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.