JSON数组未显示所有值

时间:2013-11-29 08:25:05

标签: android json arrays

我正在使用JSONParser.java来显示来自网址的JSON数据

网址和数据都是正确的,它们会显示一些JSON值(多个)

但为什么这只显示一个值?

ArrayList<HashMap<String, String>> artikelList = new ArrayList<HashMap<String, String>>();
        JSONParser jParser = new JSONParser();
        JSONObject json = jParser.getJSONFromUrl(ArtikelURL);

        try {
            artikels = json.getJSONArray(TAG_ARTIKEL);

            for(int i = 0; i < artikels.length(); i++){
                JSONObject c = artikels.getJSONObject(i);

                // Storing each json item in variable
                String tanggal = c.getString(TAG_TGL);
                String judul = c.getString(TAG_JUDUL);
                String kutipan = c.getString(TAG_KUTIPAN);

                HashMap<String, String> map = new HashMap<String, String>();            
                map.put(TAG_TGL, tanggal);
                map.put(TAG_JUDUL, judul);
                map.put(TAG_KUTIPAN, kutipan);
                artikelList.add(map);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

这是我的JSON数据:

{
   "status":"1",
   "total":20,
   "artikel":[
      {
         "tanggal":"2013-08-07",
         "judul":"Article One",
         "kutipan":"Example of article quote..."
      },
      {
         "tanggal":"2013-07-23",
         "judul":"Article Two",
         "kutipan":"Example of article quote......"
      },
      {
         "tanggal":"2013-07-22",
         "judul":"Article Three",
         "kutipan":"Example of article quote......"
      },
      {
         "tanggal":"2013-03-16",
         "judul":"Article Four"",
         "kutipan":"Example of article quote,..."
      }
   ]
}

2 个答案:

答案 0 :(得分:1)

您的JSON无效,您的阵列第3个对象的第22行出现错误

  “judul”:“第四条”,

正确的JSON是

{
"status": "1",
"total": 20,
"artikel": [
    {
        "tanggal": "2013-08-07",
        "judul": "Article One",
        "kutipan": "Example of article quote..."
    },
    {
        "tanggal": "2013-07-23",
        "judul": "Article Two",
        "kutipan": "Example of article quote......"
    },
    {
        "tanggal": "2013-07-22",
        "judul": "Article Three",
        "kutipan": "Example of article quote......"
    },
    {
        "tanggal": "2013-03-16",
        "judul": "Article Four",
        "kutipan": "Exampleofarticlequote,..."
    }
]
}

好的,现在您正在获取与网络相关的异常,所以解决方法是,您必须使用演示代码,如

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    new NetworkThread().execute();
}

class NetworkThread extends AsyncTask<Void, Void, Void> {


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

        ArrayList<HashMap<String, String>> artikelList = new ArrayList<HashMap<String, String>>();
        JSONParser jParser = new JSONParser();
        JSONObject json = jParser.getJSONFromUrl(ArtikelURL);

        try {
            JSONArray artikels = json.getJSONArray("artikel");

            for(int i = 0; i < artikels.length(); i++){
                JSONObject c = artikels.getJSONObject(i);

                // Storing each json item in variable
                String tanggal = c.getString(TAG_TGL);
                String judul = c.getString(TAG_JUDUL);
                String kutipan = c.getString(TAG_KUTIPAN);

                HashMap<String, String> map = new HashMap<String, String>();
                map.put(TAG_TGL, tanggal);
                map.put(TAG_JUDUL, judul);
                map.put(TAG_KUTIPAN, kutipan);
                artikelList.add(map);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }

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

    }
}

不要忘记将其添加到AndroidManifest.xml文件中:

<uses-permission android:name="android.permission.INTERNET"/>

答案 1 :(得分:0)

您的JSON无效但假设您有一个JsonObject的外部Json数组此实现适用于您的嵌套Json

      ArrayList<HashMap<String, String>> artikelList = new ArrayList<HashMap<String, String>>();
    JSONParser jParser = new JSONParser();
    JSONObject json = jParser.getJSONFromUrl(ArtikelURL);

    try {
       JSONArray  a = json.getJSONArray(TAG_JSON);

        for(int i = 0; i < a.length(); i++){
        HashMap<String, String> map = new HashMap<String, String>();   
         JSONObject c = a.getJSONObject(i);

            map.put(TAG_TOTAL, c.getString(TAG_TOTAL));
            map.put(TAG_STATUS, c.getString(TAG_STATUS));
            artikelList.add(map);
            JSONArray  akel = e.getJSONArray(artikel);
            if(akel != null){
          for(int j=0;j<akel.length();j++){
          HashMap<String, String> map2 = new HashMap<String, String>();
          JSONObject f = akel.getJSONObject(j);
          map2.put(TAG_TGL, f.getString(TAG_TGL));
          map2.put(TAG_JUDUL, f.getString(TAG_JUDUL));
          map2.put(TAG_KUTIPAN, f.getString(TAG_KUTIPAN));
          }
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }