Eugene H
Eugene H

Reputation: 3568

Separating JSONObjects with different values but same String Key. How do I separate them?

I am trying to separate the JSONObjects that come from the same String Key "serving_description" (that is what I believe it is called) into their own list item. There are 5 "serving_description"'s but they are all different. I have it displaying all 5 in one list item and an edit text. Image at the bottom. How do I separate each value with the same Key? In this case the String Key is "serving_description" and the 5 values.

Activity

public class RecipeListActivity extends Activity {
    EditText search, ans;
    Button enter;
    ListView meow;

    ArrayList<String> listItems = new ArrayList<String>();
    ArrayAdapter<String> adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout);
        ans = (EditText) findViewById(R.id.editText2);
        enter = (Button) findViewById(R.id.button1);
        meow = (ListView) findViewById(R.id.listView1);
        adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, listItems);
        meow.setAdapter(adapter);
        enter.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                new AsyncTask<String, String, String>() {

                    @Override
                    protected String doInBackground(String... arg0) {
                        search = (EditText) findViewById(R.id.editText1);

                        String SEARCH = search.getText().toString();
                        JSONObject food = getFood(SEARCH);
                        Log.v("FATSEC", "TEST");
                        String ret = "";

                        try {

                            JSONArray foodName = food.getJSONObject("food")
                                    .getJSONObject("servings")
                                    .getJSONArray("serving");

                            for (int n = 0; n < foodName.length(); n++) {
                                JSONObject object = foodName.getJSONObject(n);
                                String shit = object
                                        .getString("serving_description");
                                Log.v("FATSEC", "" + shit);
                                ret += shit + "\n\n";
                            }

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                        return ret;
                    }

                    @Override
                    protected void onPostExecute(String result) {
                        super.onPostExecute(result);
                        // Displaying the text
                        ans.setText("Serving Size " + result);

                        listItems.add("" + result);
                        adapter.notifyDataSetChanged();

                    }

                }.execute();

            }
        });

    }

JSON

{  
   "servings":{  
      "serving":[  
         {  
            "vitamin_a":"0",
            "calcium":"2",
            "serving_description":"1 cup cooked"
         },
         {  
            "vitamin_a":"0",
            "calcium":"6",
            "serving_description":"1 cup, dry, yields"
         },
         {  
            "vitamin_a":"0",
            "calcium":"1",
            "serving_description":"1 oz, dry, yields"
         },
         {  
            "vitamin_a":"0",
            "calcium":"1",
            "serving_description":"1 serving (105 g)"
         },
         {  
            "vitamin_a":"0",
            "calcium":"1",
            "serving_description":"100 g"
         }
      ]
   },
   "food_url":"http:\/\/www.fatsecret.com\/calories-nutrition\/generic\/rice-white-cooked-regular",
   "food_type":"Generic",
   "food_name":"White Rice",
   "food_id":"4501"
}

ListView Image of Results I am trying to separate each value. ListView of Items

Upvotes: 0

Views: 448

Answers (1)

Ker p pag
Ker p pag

Reputation: 1588

shit.split(","); will do the trick it will return an array of String which you can iterate.

Upvotes: 1

Related Questions