Android如何获取以下json的数据

时间:2014-11-26 12:55:39

标签: android json

我知道获取json数据但是当json格式改变时我感到很困难,

我想在android中获取以下类型json的数据, 请任何人帮我创建像json一样的代码。

{
"status": "success",
"response": [
    {
        "id": "101",
        "week": "1",
        "day": "4",
        "date": "2014-11-26",
        "checklist_data": [
            {
                "awaken": {
                    "essential": [
                        {
                            "aw_water": "1"
                        },
                        {
                            "aw_exercise": "1"
                        }
                    ],
                    "optional": [
                        {
                            "aw_tongue_scrape": "0"
                        },
                        {
                            "aw_meditation": "0"
                        },
                        {
                            "aw_journal": "1"
                        },
                        {
                            "aw_dry_skin_brush": "1"
                        },
                        {
                            "aw_Oil_pull": "0"
                        }
                    ]
                }
            },
            {
                "breakfast": {
                    "essential": [
                        {
                            "br_smoothie": "1"
                        },
                        {
                            "br_supplements": "1"
                        },
                        {
                            "br_tulsi_tea": "1"
                        },
                        {
                            "br_water": "1"
                        }
                    ],
                    "optional": ""
                }
            }
           ]
         }
       ]
  }

1 个答案:

答案 0 :(得分:1)

这里是代码......但是不允许在这里询问整个代码......我们解决问题,如果努力似乎在你身边......

Thread thread=new Thread(new Runnable() {
            @SuppressWarnings("unused")
            public void run() 
            {
                try
                {
                    HttpClient client=new DefaultHttpClient();
                    HttpGet request=new HttpGet("http://192.168.0.30/test.js");
                    HttpResponse response=client.execute(request);
                    HttpEntity entity=response.getEntity();
                    JSONObject jsonresponse=new JSONObject(EntityUtils.toString(entity));
                    Log.i("parse", jsonresponse.toString());

                    //start parsing
                    if(jsonresponse.getString("status").equalsIgnoreCase("success"))
                    {
                        JSONArray jsonarrayresponse=jsonresponse.getJSONArray("response");
                        JSONObject jsonobjectresponse=jsonarrayresponse.getJSONObject(0);

                        String id=jsonobjectresponse.getString("id");
                        String week=jsonobjectresponse.getString("week");
                        String date=jsonobjectresponse.getString("date");
                        String day=jsonobjectresponse.getString("day");

                        JSONArray checklist=jsonobjectresponse.getJSONArray("checklist_data");
                        JSONObject checklistjsonobject1=checklist.getJSONObject(0);
                        JSONObject checklistjsonobject2=checklist.getJSONObject(1);

                        String water=checklistjsonobject1.getJSONObject("awaken").getJSONArray("essential").getJSONObject(0).getString("aw_water");
                        String excercise=checklistjsonobject1.getJSONObject("awaken").getJSONArray("essential").getJSONObject(1).getString("aw_exercise");

                        String tonguescrape=checklistjsonobject1.getJSONObject("awaken").getJSONArray("optional").getJSONObject(0).getString("aw_tongue_scrape");
                        String meditation=checklistjsonobject1.getJSONObject("awaken").getJSONArray("optional").getJSONObject(1).getString("aw_meditation");
                        String journal=checklistjsonobject1.getJSONObject("awaken").getJSONArray("optional").getJSONObject(2).getString("aw_journal");
                        String dryskinbrush=checklistjsonobject1.getJSONObject("awaken").getJSONArray("optional").getJSONObject(3).getString("aw_dry_skin_brush");
                        String oilpull=checklistjsonobject1.getJSONObject("awaken").getJSONArray("optional").getJSONObject(4).getString("aw_Oil_pull");

                        String smoothie=checklistjsonobject2.getJSONObject("breakfast").getJSONArray("essential").getJSONObject(0).getString("br_smoothie");
                        String supplements=checklistjsonobject2.getJSONObject("breakfast").getJSONArray("essential").getJSONObject(1).getString("br_supplements");
                        String tulsitee=checklistjsonobject2.getJSONObject("breakfast").getJSONArray("essential").getJSONObject(2).getString("br_tulsi_tea");
                        String water2=checklistjsonobject2.getJSONObject("breakfast").getJSONArray("essential").getJSONObject(3).getString("br_water");
                        Log.i("parse", id);
                        Log.i("parse", week);
                        Log.i("parse", date);
                        Log.i("parse", day);
                        Log.i("parse", water);

                        Log.i("parse", excercise);
                        Log.i("parse", tonguescrape);
                        Log.i("parse", meditation);
                        Log.i("parse", journal);
                        Log.i("parse", dryskinbrush);

                        Log.i("parse", oilpull);
                        Log.i("parse", smoothie);
                        Log.i("parse", supplements);
                        Log.i("parse", tulsitee);
                        Log.i("parse", water2);
                    }

                    entity.consumeContent();
                }
                catch(Exception ex)
                {
                    ex.toString();
                    Log.i("parse", ex.toString());
                }
            }
    });
    thread.start();