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
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.
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.
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.
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With