Skip to content Skip to sidebar Skip to footer

Convert String Array To Json In Android

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?

Solution 1:

Just convert use JSONArray,

import java.util.Arrays;
import org.json.JSONArray;
String[] inputs = newString[] {"foo", "bar"};
JSONArray jsonArray = newJSONArray(Arrays.asList(inputs));

Solution 2:

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.

Solution 3:

Try something like

LinkedList list = newLinkedList();
For(String s : array) {
     list.add(s);
}

String jsonText =     JSONValue.toJSONString(list)

Solution 4:

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.

Post a Comment for "Convert String Array To Json In Android"