I get data from Json and there's a Json array. I want to convert that Json array into String array, so I can send it into another activity and show it in ListView.
Here's My java code
if (jsonStr != null) {
try {
foodsFilter = new JSONArray(jsonStr);
// looping through All Contacts
for (int i = 0; i < foodsFilter.length(); i++) {
JSONObject c = foodsFilter.getJSONObject(i);
if(c.getString("category_name").equals("Food")) {
String category_name = c.getString(TAG_CATEGORY_NAME);
String filter_type = c.getString(TAG_FILTER_TYPE);
//String item_list = c.getString(TAG_ITEM_LIST);
JSONArray itemList = new JSONArray(c.getString("item_list"));
String item_list = itemList.toString();
// tmp hashmap for single contact
HashMap<String, String> filter = new HashMap<String, String>();
// adding each child node to HashMap key => value
filter.put(TAG_CATEGORY_NAME, category_name);
filter.put(TAG_FILTER_TYPE, filter_type);
filter.put(TAG_ITEM_LIST, item_list);
// adding contact to contact list
foodsFilterList.add(filter);
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
I try that code to convert the JSONarray, but I realized that code is for convert the JSONArray into String.
Here's my JSON data
[{"category_name":"Food","filter_type":"Sort by","field_name":"","type":"VALUE","table_name":"","item_list":["Ascending","Descending"]}]
I want to convert the item_list Array into like this
item_list = {"Ascending", "Descending"}
So I can send it into another activity use Intent and show it in ListView
-
Why not simply do this on the listview activity itself? Passing stuff using Intent is an expensive operationLeonardo– Leonardo2014年07月25日 03:32:13 +00:00Commented Jul 25, 2014 at 3:32
-
Because I already have a listview on that activity. And I want to show that Data in another activity :DMatthew– Matthew2014年07月25日 03:40:47 +00:00Commented Jul 25, 2014 at 3:40
-
@Matthew you can still parse the json in the listview activity itself.Raghunandan– Raghunandan2014年07月25日 03:48:33 +00:00Commented Jul 25, 2014 at 3:48
-
@Raghunandan Do you mean, show the data on the same activity?Matthew– Matthew2014年07月25日 04:06:35 +00:00Commented Jul 25, 2014 at 4:06
-
@Matthew you could show in the same activity or parse the data in Activity that has listview. passing through intent is expensive operation. If you can avoid it goodRaghunandan– Raghunandan2014年07月25日 04:56:52 +00:00Commented Jul 25, 2014 at 4:56
2 Answers 2
What you have
String item_list = itemList.toString();
You need to parse items_list which is a JSONArray.
JSONArray itemList = new JSONArray(c.getString("item_list"));
// loop through the array itemList and get the items
for(int i=0;i<itemList.length();i++)
{
String item = itemList.getString(i); // item at index i
}
Now you can add the strings to a list/array and then do what is required.
5 Comments
String item = itemsList.getString(i); // item at index i that the itemList variable cannot be resolved.itemList is JSONArray itemList = new JSONArray(c.getString("item_list"));Please have a look on this tutorial.
http://www.androidhive.info/2012/01/android-json-parsing-tutorial/
Maybe this would help you.
ArrayList<String> stringArray = new ArrayList<String>();
JSONArray jsonArray = new JSONArray();
for(int i = 0, count = jsonArray.length(); i< count; i++)
{
try {
JSONObject jsonObject = jsonArray.getJSONObject(i);
stringArray.add(jsonObject.toString());
}
catch (JSONException e) {
e.printStackTrace();
}
}