Arshad Ali
Arshad Ali

Reputation: 3274

could not be able to get data from json webservice

I want to get data from a jason webservice,

JSON response is :

{"content":[{"id":"1","asset_id":"62","title":"sample page","alias":"","introtext":"","fulltext":"Some Contents"},{"id":"2","asset_id":"62","title":"sample page2","alias":"","introtext":"","fulltext":"Some Contents"},{"id":"3","asset_id":"62","title":"sample page3","alias":"","introtext":"","fulltext":"Some Contents"}]}

After Visiting Here I have done in this way:

private void parseData() {
    // Creating JSON Parser instance
    JSONParser jParser = new JSONParser();

    // getting JSON string from URL
    JSONObject json = jParser.getJSONFromUrl(BASE_URL);

    try {
        // Getting Array of Contents
        contents = json.getJSONArray(TAG_CONTENTS);

        // looping through All Contents
        for(int i = 0; i < contents.length(); i++){
            JSONObject c = contents.getJSONObject(i);

            // Storing each json item in variable
            id = c.getString(TAG_ID);
            title = c.getString(TAG_TITLE);      
        }

        textView.setText(id + " " + title);
    } catch (JSONException e) {
        e.printStackTrace();
    }
}

Now I got id = 3 and title = sample page3result now how can I get first two values as also!!?

Upvotes: 0

Views: 191

Answers (3)

user2122397
user2122397

Reputation:

Arshay!! Try This One Man!!

private void parseData() {
    // Creating JSON Parser instance
    MyJSONParser jParser = new MyJSONParser();

    // getting JSON string from URL
    JSONObject json = jParser.getJSONFromUrl(BASE_URL);

    try {
        // Getting Array of Contents
        jsonArrar = json.getJSONArray(TAG_CONTENTS);
        List list = new ArrayList<String>();
        // looping through All Contents
        for(int i = 0; i < jsonArrar.length(); i++){
           // JSONObject c = jsonArrar.getJSONObject(i);


            String id1=jsonArrar.getJSONObject(i).getString(TAG_ID);                
            String title=jsonArrar.getJSONObject(i).getString(TAG_TITLE);               
            String fullText=jsonArrar.getJSONObject(i).getString(TAG_FULL_TEXT);                

            list.add(id1);
            list.add(title);
            list.add(fullText);
        }

        Iterator<String> iterator = list.iterator();
        StringBuilder builder = new StringBuilder();
        while (iterator.hasNext()) {
            String string = iterator.next();                
            builder.append(string+"\n");

        }

        textView.setText(builder);
    } catch (JSONException e) {
        e.printStackTrace();
    }
}

Upvotes: 1

mmoghrabi
mmoghrabi

Reputation: 1291

Try something like this

// jsonData : response

List< String> contents = new ArrayList< String>();

String[] val;

            try {


        JSONObject jsonObj = new JSONObject(jsonData);
        if (jsonObj.get(JSON_ROOT_KEY) instanceof JSONArray) {

            JSONArray array = jsonObj.optJSONArray(JSON_ROOT_KEY);
            for (int loop = 0; loop < array.length(); loop++) {
val = new String[loop];
JSONObject Jsonval = array.getJSONObject(loop);
val.Jsonval.getString(TAG_ID);
val.Jsonval.getString(asset_id);

. . etc

contents.add(val);

}
}

}

Upvotes: 0

Your line is JSONObject and not JSONArray.

You should use it like that:

JSONObject jso = new JSONObject(line);
JSONArray jsa = new JSONArray(jso.getJSONArray("content"));

Upvotes: 0

Related Questions