Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java convert a Json string to an array

Tags:

java

json

android

I have a Json String and I am trying to convert it to an array in Java.

public void DisplaySubjects(String subjects)
     {
         JSONObject jsonResponse;
         jsonResponse = new JSONObject(subjects));

Thats as far as I get.I'm not even sure if I have to create a object first.

What I will need to do eventuallay is attach it to a ArrayAdapter in an android app.

Thanks

like image 707
mike628 Avatar asked Jul 17 '12 16:07

mike628


People also ask

Can we convert JSON to array?

Convert JSON to Array Using `json. The parse() function takes the argument of the JSON source and converts it to the JSON format, because most of the time when you fetch the data from the server the format of the response is the string. Make sure that it has a string value coming from a server or the local source.

How do you represent a JSON array of strings?

It can store string, number, boolean or object in JSON array. In JSON array, values must be separated by comma. The [ (square bracket) represents JSON array.

What is a JsonArray?

JsonArray represents an immutable JSON array (an ordered sequence of zero or more values). It also provides an unmodifiable list view of the values in the array. A JsonArray object can be created by reading JSON data from an input source or it can be built from scratch using an array builder object.


1 Answers

Something like this:

ArrayList<String> jsonStringToArray(String jsonString) throws JSONException {

    ArrayList<String> stringArray = new ArrayList<String>();

    JSONArray jsonArray = new JSONArray(jsonString);

    for (int i = 0; i < jsonArray.length(); i++) {
        stringArray.add(jsonArray.getString(i));
    }

    return stringArray;
}
like image 170
Prizoff Avatar answered Sep 26 '22 01:09

Prizoff