CodePrimate
CodePrimate

Reputation: 6666

How do I pull the string array from this json object?

I am trying to get a list of available numbers from the following json object, using the class from org.json

    {
        "response":true,
        "state":1,
        "data":
        {
            "CALLERID":"81101099",
            "numbers":
                [
                       "21344111","21772917",
                       "28511113","29274472",
                       "29843999","29845591",
                       "30870001","30870089",
                       "30870090","30870091"
                ]
        }
    }

My first steps were, after receiving the json object from the web service:

jsonObj = new JSONObject(response);
jsonData = jsonObj.optJSONObject("data");

Now, how do I save the string array of numbers?

Upvotes: 14

Views: 54978

Answers (4)

jeet
jeet

Reputation: 29199

use:

jsonObj = new JSONObject(response);
jsonData = jsonObj.optJSONObject("data");
JSONArray arrJson = jsonData.getJSONArray("numbers");
String[] arr = new String[arrJson.length()];
for(int i = 0; i < arrJson.length(); i++)
    arr[i] = arrJson.getString(i);

Upvotes: 43

QuartZ
QuartZ

Reputation: 164

My code is for getting "data":

public void jsonParserArray(String json) {

        String [] resultsNumbers = new String[100];

        try {
            JSONObject jsonObjectGetData = new JSONObject(json);
            JSONObject jsonObjectGetNumbers = jsonObjectGetData.optJSONObject("results");
            JSONArray jsonArray = jsonObjectGetNumbers.getJSONArray("numbers");
            for (int i = 0; i < jsonArray.length(); i++) {
                resultsNumbers[i] = jsonArray.getString(i);
            }
        } catch (JSONException e) {
            e.printStackTrace();
            Log.e(LOG_TAG, e.toString());
        }
    }

Upvotes: 1

waqaslam
waqaslam

Reputation: 68177

you need to use JSONArray to pull data in an array

JSONObject jObj= new JSONObject(your_json_response);
JSONArray array = jObj.getJSONArray("data");

Upvotes: 3

ahsan_cse2004
ahsan_cse2004

Reputation: 160

Assuming that you are trying to get it in a javascript block, Try something like this

var arrNumber = jsonData.numbers;

Upvotes: 0

Related Questions