Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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

share|improve this question
    
Why not simply do this on the listview activity itself? Passing stuff using Intent is an expensive operation –  Leonardo Ferrari Jul 25 '14 at 3:32
    
Because I already have a listview on that activity. And I want to show that Data in another activity :D –  Matthew Jul 25 '14 at 3:40
    
@Matthew you can still parse the json in the listview activity itself. –  Raghunandan Jul 25 '14 at 3:48
    
@Raghunandan Do you mean, show the data on the same activity? –  Matthew Jul 25 '14 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 –  Raghunandan Jul 25 '14 at 4:56

2 Answers 2

up vote 3 down vote accepted

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.

share|improve this answer
    
What If I have a dinamic data? Not just Ascend and Descend? –  Matthew Jul 25 '14 at 3:26
    
@Matthew you still be looping through the array from 0 to end of array and use the index to get the items required –  Raghunandan Jul 25 '14 at 3:27
    
there's an error on String item = itemsList.getString(i); // item at index i that the itemList variable cannot be resolved. –  Matthew Jul 25 '14 at 4:04
    
@Matthew where do you have that piece of code?. itemList is JSONArray itemList = new JSONArray(c.getString("item_list")); –  Raghunandan Jul 25 '14 at 4:55
1  
Thanks for your help. Its done. I really appreciate it –  Matthew Jul 25 '14 at 6:08

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();
    }
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.