2

I have a problem. I want to pass String array to javascript, but this is impossible, so I want to convert String array to json and send it to java script. How can I do that?

asked Aug 20, 2013 at 5:35

4 Answers 4

15

Just convert use JSONArray,

import java.util.Arrays;
import org.json.JSONArray;
String[] inputs = new String[] {"foo", "bar"};
JSONArray jsonArray = new JSONArray(Arrays.asList(inputs));
answered Aug 20, 2013 at 6:03
Sign up to request clarification or add additional context in comments.

Comments

1

A popular solution for Android would be the use of GSON: https://code.google.com/p/google-gson/

The package provides simple toJson and fromJson methods on its main converter object, so you get there quite easily. Alternatives like Jackson often require some additional preparation of the converted objects.

answered Aug 20, 2013 at 5:40

Comments

0

Try something like

LinkedList list = new LinkedList();
For(String s : array) {
 list.add(s);
}
String jsonText = JSONValue.toJSONString(list)
answered Aug 20, 2013 at 5:41

Comments

0

You could try using FlexJSON for Serialization/Deserialization of objects. It has a nice and easy tutorial accessible on the website and is flexible in its usage.

answered Aug 20, 2013 at 5:52

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.