1
  • I'm trying to get data from json. I can get data at first state.
  • But how to get data "ascending" and "descending" and show it on another activity in listview ?

Here's My Json

[{"category_name":"Food","filter_type":"Sort by","field_name":"","type":"VALUE","table_name":"","item_list":["Ascending","Descending"]}

And here's my Java code

if (jsonStr != null) {
    try {

        foods = new JSONArray(jsonStr);


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

            if(c.getString("category_name").equals("Food")) {

                String category_name = c.getString(TAG_CATEGORY_NAME);
                String table_name = c.getString(TAG_TABLE_NAME);
                String item_list = c.getString(TAG_ITEM_LIST);

                // tmp hashmap for single contact
                HashMap<String, String> contact = new HashMap<String, String>();

                // adding each child node to HashMap key => value
                contact.put(TAG_CATEGORY_NAME, category_name);
                contact.put(TAG_TABLE_NAME, table_name);
                contact.put(TAG_ITEM_LIST, item_list);

                // adding contact to contact list
                foodlistfilter.add(contact);

            }
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }
} else {
    Log.e("ServiceHandler", "Couldn't get any data from the url");
}

I'm trying to follow this tutorial http://www.androidhive.info/2012/01/android-json-parsing-tutorial/, but i still don't fully understand.

Jeremy Cook
  • 20,840
  • 9
  • 71
  • 77
Matthew
  • 95
  • 4
  • 14

3 Answers3

1

To get a JSONArray from your JSONObject c simply write:

JSONArray itemList = c.getJSONArray(name);

then you can iterate through that data like any other array

for (int i = 0; i < itemList.length(); i++) {
  // do something
}
erik
  • 4,946
  • 13
  • 70
  • 120
  • Could you please add some explanation to your answer? Code-only answers are (sometimes) good, but code+explanation answers are (most times) better – Barranka Jul 23 '14 at 18:46
1

Let me explain this.

[ means its an array. { is an object.

In your case it's an array whcih contains an -JSONObject with name category_name, filter_type and field_name. type and table_name and a new jsonarray with object item_list.

How can you parse this string?

Here is an example:

String str = "[{"category_name":"Food","filter_type":"Sort by","field_name":"","type":"VALUE","table_name":"","item_list":["Ascending","Descending"]}";

JSONArray jsonArray = new JSONArray(str);
//now it holds the JSONObject.
for (int i = 0; i<= jsonArray.length(); i++) {
 //now we loop through and get the jsonObject
 JSONObject jsonObj = new JSONObject(jsonArray.getJsonObject(i));
 //now it contains your data.
 Log.d("Category_nameValue=", jsonObj.getString("category_name"));
 //now we want to get the array from the item_list. 
 JSONArray itemList = new JSONArray(jsonObj.getString("item_list")); 
 //now itemList.getString(1);  === Ascending while itemList.getString(2) == Descending



 //now itemList contains several new objects which can also be looped as the parent one.
}

Since you now know how to create an JSONArray, you can start sorting it.

This has been answered already at Android how to sort JSONArray of JSONObjects

If you want to send those data to another Activity you can use the JSONArray.toString() method and send it via Intents.

This is easy explained at Pass a String from one Activity to another Activity in Android

Hope this helps.

Community
  • 1
  • 1
Emanuel
  • 8,027
  • 2
  • 37
  • 56
  • Thanks for explanation. But what I mean is not sorting the data. If you see the item_list, there're two data ( Ascending and Descending ) inside the array. What I want is get that data, and show it on another activity in listview. – Matthew Jul 23 '14 at 17:42
  • Thanks a lot :D. Now What I have to do is just make a list from an intent, right. – Matthew Jul 23 '14 at 17:51
1

If you're new, I would recommend you think about using Gson to parse your Json response directly to a java entity class. So you will avoid to manually parse all your responses.

Your JSON response

[{"category_name":"Food","filter_type":"Sort by","field_name":"","type":"VALUE","table_name":"","item_list":["Ascending","Descending"]}

The entity representing the response

public class MyEntity {
    String category_name;
    String filter_type;
    String field_name;
    String type;
    String table_name;
    String [] item_list;

   // getters / setters ...
}

Parsing the response

Gson gson = new Gson();
MyEntity myEntity = gson.fromJson(response, MyEntity.class);

Finally, to send the data, start the new Activity with extras

Intent intent = new Intent(this, AnotherActivity.class);
intent.putExtra("EXTRA_DATA", myEntity.getCategoryName());
startActivity(intent);

Now you recover the extra data on your AnotherActivity

Intent intent = getIntent();
String categoryName = intent.getStringExtra("EXTRA_DATA");

And you can fill the ListView using an ArrayAdapter: Example

David Gras
  • 958
  • 2
  • 12
  • 21