8

I am trying to add string values in JsonArray and after that create Json String to send it on server.

I was searching on google but I couldn't find anything helpful. Could anyone tell me how to this? Thanks

 JSONArray list = new JSONArray();
 list.put("Hello");
 list.put("Hey");

After that I want create JSONString

2
  • Can you post your code that you have been trying with? Commented Jun 15, 2012 at 15:16
  • According to docs it seems that you do it the right way: json.org/javadoc/org/json/JSONArray.html#put(boolean). How do you check that it's not added? Commented Jun 15, 2012 at 15:22

2 Answers 2

16

Simple:

JSONArray list = new JSONArray();
list.put("Hello");
list.put("Hey");

String jsonString = list.toString()

according to docs it should result in "["Hello", "Hey"]"

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

Comments

1

just use

list.toString(2)

this will give you the string in json

2 Comments

He wanted to add String, not to retrieve it
after using his code use list.toString to get the JSON-String

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.