Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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

share|improve this question
Relevant : stackoverflow.com/questions/5245840/… – SidCool Jul 17 '12 at 16:59

2 Answers

up vote 2 down vote accepted

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;
}
share|improve this answer

You could try something like:

new JSONArray(jsonString)

or if it is a property:

jsonObject.getJSONArray(propertyName)
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.