简体   繁体   中英

Android / Java : Parse Array of JSON coming from Rails

My server is written in Rails 4.x and to the to_json method I'm using to return records back through the API is delivering an array of JSON to Android that looks like this:

[{"id":503240,"name":"bob"},{"id":503241,"name":"Tom"}]

It comes down to Android fine and I have no problems using it as a string and logging it.

Now I want to loop through each item of the array grab the id (ultimately to show it on a ListView).

The first thing I need to do is "see" each individual object. So I am parsing the above using the following line of code:

JSONArray jsonResponse = new JSONArray(jsonString);

It "parses" with no errors, but ... when I try to do something like jsonResponse[1], or jsonResponse.getObject(1) to get my hands on the one of the elements of the array (just to log it for example), I can't get past the IDE telling me I'm doing it wrong.

If I can address and manipulate the individual json objects, I'm sure I'll have no problems addressing the individual data elements. It's the arrayness of this that is causing problems.

I'm a little new to Java and I'm spoiled by Ruby and Swift. I'm missing something basic. What am I doing wrong?

Thanks in advance.

There seems to be no method getObject on JSONArray instances in Android. Documentation here .

I guess what you need is:

jsonResponse.getJSONObject(1)

because jsonResponse is an Array of JSONObject s

JSONArray contains a method called JSONObject getJSONObject(int position) , which returns the item of type JSONObject which is found in the JSONArray at the position you asked for.

Now if you want to populate the json array into a listview, you should create an adapter for the listview and each row will contain the data of each item from the array.

The most common adapter is extending the BaseAdapter class, you will notice that you will need to implement some methods, the main are

  • getCount - which usually returns the number of items you have in the array, so basically when you return 0 , you will see nothing, but if you return 1 or the size of the array - the listview will have the same number of rows.
  • getView - which returns the view of each row, in this method you will need to inflate the view of the row (you will need to create an xml file that will be the layout of the row), after you inflate the row and all of the elemnts like textview, imageview , you will need to populate the data from each item in your array. the mechanism works in this way: one of the arguments of this method is the position of the row, so when the user is scrolling the list, this method will be called and the position will be updated automatically, in that case to get the specific item from your array you just need to call yourJSONArray.getJSONObject(position) , and then fill each text field with the data you want.

Notice that in order to create some usefull performence there is something called ViewHolder, you should create this one in order to avoid from android to create & inflate the row-view each time when the user is scrolling.

One more thing is that you can also read and check about recycleview.

To loop through a JSONArray just use a for loop like this

try {
    JSONArray array = new JSONArray("[{\"id\":503240,\"name\":\"bob\"},{\"id\":503241,\"name\":\"Tom\"}]");

    for (int i = 0; i < array.length(); i++) {
        JSONObject object = array.getJSONObject(i);

        Log.d("TAG", "Object #" + i + " id: " + object.getInt("id") + " name: " + object.getString("name"));
    }

} catch (JSONException e) {
    Log.e("TAG", "Cannot parse json :", e);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM