Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting specific value from JSONArray

noob Android/JSON person here. I hope someone might help me?

I've looked and looked but don't think it's what I'm after. I've been working on this project all day so maybe my brain has just gone to mush... If this has been awnsered else where please point me that way :)

Anyway, I wish to get a specific object from within an JSONArray - here's what's happening so far:

  JSONArray jArray = new JSONArray(result);
                for(int i=0;i<jArray.length();i++){
                        JSONObject json_obj = jArray.getJSONObject(i);

                        name = json_obj.getString("txt_title");

                }

                txt_title.setText(name);

As far as I understand result returns the entire JSONArray, then I go through the length of those results using the for loop and get the json objects. At the moment I'm only asking for values from "txt_title" in the Array. So far, so good?

Then what I want to do is, say only set the third "txt_title" value from the Array.

At the moment I would expect txt_title.setText(name) to be displaying ALL the titles in "txt_title" however it's only displaying the LAST title in the Array. This probably has something to do with the for loop?

How would I go about choosing which object is displayed?

like image 384
Willis Avatar asked Apr 01 '12 00:04

Willis


People also ask

How get values from JSONArray?

You can get the value using the line: String value = (String) getKey(array, "key1") . We cast to a string because we know "key1" refers to a string object.

What does JSONArray contain?

A JSON array contains zero, one, or more ordered elements, separated by a comma. The JSON array is surrounded by square brackets [ ] . A JSON array is zero terminated, the first index of the array is zero (0). Therefore, the last index of the array is length - 1.

What is the difference between JSONObject and JSONArray?

JSONObject and JSONArray are the two common classes usually available in most of the JSON processing libraries. A JSONObject stores unordered key-value pairs, much like a Java Map implementation. A JSONArray, on the other hand, is an ordered sequence of values much like a List or a Vector in Java.

How is JSONArray defined?

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.


1 Answers

You are only displaying the last one in the list right now because you are setting name each time in the loop.

name = json_obj.getString("txt_title");

this overwrites the previous value every time you iterate. If you want to have all the values, you would have to do it in an additive way.

name += json_obj.getString("txt_title");

If you want to get a specific item from the array you just need to access it using the index you want instead of a loop.

    if(jArray.length() > 2) {
         JSONObject json_obj = jArray.getJSONObject(2);   //get the 3rd item
         name = json_obj.getString("txt_title");
    }

Hope that helps you understand how to access it.

like image 73
Brian ONeil Avatar answered Nov 02 '22 10:11

Brian ONeil