Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

json

android

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?

like image 432
CodePrimate Avatar asked Feb 21 '12 06:02

CodePrimate


People also ask

How can we get JSON array from JSON string?

JSONObject jobj=new JSONObject(response); String c = jobj. getString("GetDataResult"); JSONArray jArray = new JSONArray(c); deviceId=jArray. getJSONObject(0). getString("DeviceID");

How do I get JSON data into an array?

Approach 1: First convert the JSON string to the JavaScript object using JSON. Parse() method and then take out the values of the object and push them into the array using push() method.

How do you parse an array of JSON objects?

String value = (String) jsonObject. get("key_name"); Just like other element retrieve the json array using the get() method into the JSONArray object.


4 Answers

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);
like image 191
jeet Avatar answered Oct 13 '22 08:10

jeet


you need to use JSONArray to pull data in an array

JSONObject jObj= new JSONObject(your_json_response);
JSONArray array = jObj.getJSONArray("data");
like image 38
waqaslam Avatar answered Oct 13 '22 09:10

waqaslam


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

var arrNumber = jsonData.numbers;

like image 41
ahsan_cse2004 Avatar answered Oct 13 '22 09:10

ahsan_cse2004


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());
        }
    }
like image 22
QuartZ Avatar answered Oct 13 '22 09:10

QuartZ