从json恢复多个数据

时间:2013-06-05 10:00:06

标签: android json

我正在尝试解析HashMap中的json以使用我的应用程序中的数据。

以下是代码的简化版本:

        JSONArray array1 = new JSONArray(json);
        for (int i = 0; i < array1.length(); i++) {
            HashMap<String, String> map = new HashMap<String, String>();
            JSONObject object1 = array1.getJSONObject(i);
            JSONObject object2 = object1.getJSONObject("job");

            JSONArray array2 = object2.getJSONArray("formations");

            if (array2.length() != 0) {
                for (int i1 = 0; i1 < array2.length() - 1; i1++) {
                    JSONObject object3 = array2.getJSONObject(i1);
                    map.put("formation-created_at",

                }
            }
            map.put("testimony", object2.getString("testimony"));

            JSONArray array3 = object2.getJSONArray("themes");

            if (array3.length() != 0) {
                for (int i2 = 0; i2 < array3.length() - 1; i2++) {
                    JSONObject object4 = array3.getJSONObject(i2);

                    map.put("themes-intro", object4.getString("intro"));
                }
            }

            JSONObject object5 = object2.getJSONObject("sector");

            map.put("sector-description", object5.getString("description"));
            list.add(map);
        }

        Log.d("testimony", list.get(1).get("testimony"));
        Log.d("themes-intro", list.get(1).get("themes-intro"));

我可以使用以下方法恢复地图中的第一个对象:

Log.d("testimony", list.get(1).get("testimony"));

但是我无法恢复循环中的人:

Log.d("themes-intro", list.get(1).get("themes-intro"));

logcat返回错误:

FATAL EXCEPTION: main
java.lang.NullPointerException: println needs a message
at android.util.Log.println_native(Native Method)
at android.util.Log.d(Log.java:138)

我知道我应该指定我想从循环中调用哪些“themes-intro”,但我不能在表达式中添加.get(x)。

编辑:

以下是完整的json:http://komfushee.com/documents/ctai/jobs.json

我编辑了我的代码以提供完整版本的代码

1 个答案:

答案 0 :(得分:0)

请使用下面的代码,我想我希望它会变成单身......

HashMap<String, String> map1 = new HashMap<String, String>();
    HashMap<String, String> map2 = new HashMap<String, String>();

    if (array1.length() != 0) {

        for (int i = 0; i < array1.length(); i++) {

            JSONObject object1 = array1.getJSONObject(i);
            JSONObject object2 = object1.getJSONObject("job");
            map1.put("testimony", object2.getString("testimony"));

        }
    }

    if (array2.length() != 0) {
        for (int i1 = 0; i1 < array2.length() - 1; i1++) {
            JSONObject object3 = array2.getJSONObject(i1);
            map2.put("formation-created_at", object3.getString("created_at"));
        }

    }