1

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

9
  • Why not simply do this on the listview activity itself? Passing stuff using Intent is an expensive operation Commented 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 :D Commented Jul 25, 2014 at 3:40
  • @Matthew you can still parse the json in the listview activity itself. Commented Jul 25, 2014 at 3:48
  • @Raghunandan Do you mean, show the data on the same activity? Commented 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 good Commented Jul 25, 2014 at 4:56

2 Answers 2

5

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.

Sign up to request clarification or add additional context in comments.

5 Comments

What If I have a dinamic data? Not just Ascend and Descend?
@Matthew you still be looping through the array from 0 to end of array and use the index to get the items required
there's an error on String item = itemsList.getString(i); // item at index i that the itemList variable cannot be resolved.
@Matthew where do you have that piece of code?. itemList is JSONArray itemList = new JSONArray(c.getString("item_list"));
Thanks for your help. Its done. I really appreciate it
1

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();
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.