如何解析Android中显示为foders的JsonObject节目?

时间:2015-03-02 13:34:16

标签: android arrays json jsonobject

这是我的json回复 result: [ { folder1: [ { test1.1: [ { app1.1.1: [ ] }, { app1.1.2: [ ] } ] }, { test1.2: [ { app1.2.1: [ ] }, { app1.2.2: [ ] } ] } ] }, { folder2: [ { test2.1: [ { app2.1.1: [ ] }, { app2.1.2: [ ] } ] }, { test2.2: [ { app2.2.1: [ ] }, { app2.2.2: [ ] } ] } ] }, { folder3: [ { test3.1: [ { app3.1.1: [ ] }, { app3.1.2: [ ] } ] }, { test3.2: [ { app3.2.1: [ ] }, { app3.2.2: [ ] } ] } ] } ] }

这是我的代码

`类Getdata扩展了AsyncTask {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(ExampleActivity.this);
        pDialog.setMessage("Please wait...");
        pDialog.setCancelable(false);
        pDialog.show();
    }

    @Override
    protected Void doInBackground(Void... arg0) {

        if (pref.getString("instraw", null) != null) {
            Log.e("Enter  Not Null>>>", "Not Null");
            spString = pref.getString("instraw", null);
            jsonRes = spString;
            getServerData();
        } else {
            Log.e("Enter Null>>>", "Yes It's Null");
            ServiceHandler handler = new ServiceHandler();
            jsonRes = handler.makeServiceCall(url, ServiceHandler.GET);
            editor.putString("instraw", jsonRes);
            editor.commit();
            getServerData();
        }
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);

        if (pDialog.isShowing())
            pDialog.dismiss();

        listView.setAdapter(new ArrayAdapter<>(ExampleActivity.this,
                R.layout.schema_item, R.id.textView1, list1));

    }

}

private void getServerData() {

    Log.e("ENTER-getServerData>>>>", "0000000");

    if (jsonRes != null) {

        try {
            mainObj = new JSONObject(jsonRes);

            jsonArray = mainObj.getJSONArray("result");
            Log.i("MAIN_ARRAY>>>>", jsonArray + "");
            for (int i = 0; i < jsonArray.length(); i++) {

                JSONObject Obj = jsonArray.getJSONObject(i);
                Log.i("MAIN-OBJ>>>>", "" + Obj);
                getObjMeth(Obj);
            }

        } catch (Exception e) {

        }

    } else {

    }

}

private void getObjMeth(JSONObject obj) {

    Log.e("ENTER-getObjMeth>>>>", "1111-->getObjMeth");

    try {

        Iterator<String> i_keys = obj.keys();

        while (i_keys.hasNext()) {
            String strkey = i_keys.next();
            Log.e("getObjMeth-key>>>>", strkey);
            list1.add(strkey);
            Log.e("<<keys-list1>>", "" + list1);


                sub_sub_Obj = new JSONObject();
                sub_sub_Obj = obj;


            listView.setAdapter(new ArrayAdapter<>(ExampleActivity.this,
                    R.layout.schema_item, R.id.textView1, list1));

        }

    } catch (Exception e) {

    }

}

private void methFirst1(String list3) {

    Log.e("ENTER-methFirst1>>>>", "2222---> " + list3);
    Log.e("SUB-ARRAY>>>>", "2222---> " + subArray);

    try {



        subArray = sub_sub_Obj.getJSONArray(list3);

        Log.e("size>>>>>>>>>>>", "" + subArray.length());

        for (int j = 0; j < subArray.length(); j++) {

            Log.i("Test----->>>>>>", "test--" + j);

            JSONObject Obj = subArray.getJSONObject(j);

            Log.e("SUB-OBJ>>>>>>>>>>>", "" + Obj);

            getObjMeth(Obj);

            // break;

        }

    } catch (Exception e) {

    }

}

` 我希望解析上面的响应并显示为文件夹(文件夹1有foleder1.1和test1.2在那里,在文件夹1.1中有app1.1.1和app1.1.2)这样请帮助我谢谢

2 个答案:

答案 0 :(得分:0)

检查你的Json {{3p>

- &GT;您的Json格式错误它给错误更改您的json Formate

答案 1 :(得分:0)

{ "result": [ { "folder1": [ { "test1.1": [ { "app1.1.1": [] }, { "app1.1.2": [] } ] }, { "test1.2": [ { "app1.2.1": [] }, { "app1.2.2": [] } ] } ] }, { "folder2": [ { "test2.1": [ { "app2.1.1": [] }, { "app2.1.2": [] } ] }, { "test2.2": [ { "app2.2.1": [] }, { "app2.2.2": [] } ] } ] }, { "folder3": [ { "test3.1": [ { "app3.1.1": [] }, { "app3.1.2": [] } ] }, { "test3.2": [ { "app3.2.1": [] }, { "app3.2.2": [] } ] } ] } ] }

当我在www.jsonlint.com验证时,你的json结构会出错。

尝试使用给定的JSON结构。