JSON阵列不显示所有值 [英] JSON Array Not Showing All Values

查看:140
本文介绍了JSON阵列不显示所有值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用JSONParser.java显示来自一个URL JSON数据

URL和数据是正确的,他们表现出JSON的一些值(不止一个)

但为什么这个节目只有一个值?

 的ArrayList<&HashMap的LT;字符串,字符串>> artikelList =新的ArrayList<&HashMap的LT;字符串,字符串>>();
        JSONParser jParser =新JSONParser();
        JSONObject的JSON = jParser.getJSONFromUrl(ArtikelURL);        尝试{
            artikels = json.getJSONArray(TAG_ARTIKEL);            的for(int i = 0; I< artikels.length();我++){
                JSONObject的C = artikels.getJSONObject(I)                //存储在变量中的每个JSON项目
                字符串tanggal = c.getString(TAG_TGL);
                字符串judul = c.getString(TAG_JUDUL);
                字符串kutipan = c.getString(TAG_KUTIPAN);                HashMap的<字符串,字符串>地图=新的HashMap<字符串,字符串>();
                map.put(TAG_TGL,tanggal);
                map.put(TAG_JUDUL,judul);
                map.put(TAG_KUTIPAN,kutipan);
                artikelList.add(地图);
            }
        }赶上(JSONException E){
            e.printStackTrace();
        }

这是我的JSON数据:

 {
   状态:1
   总:20,
   ARTIKEL:[
      {
         tanggal:2013年8月7日,
         judul:壹条
         kutipan:文章引用的例子......
      },
      {
         tanggal:2013年7月23日,
         judul:第二条
         kutipan:文章引用的例子......
      },
      {
         tanggal:2013年7月22日,
         judul:第三条
         kutipan:文章引用的例子......
      },
      {
         tanggal:2013年3月16日,
         judul:第四条",
         kutipan:文章引用的例子,...
      }
   ]
}


解决方案

您JSON是无效的,有一个在你的数组的第3对象在第22行错误


  

judul:第四条


正确的JSON是

  {
状态:1
总:20,
ARTIKEL:[
    {
        tanggal:2013年8月7日,
        judul:壹条
        kutipan:文章引用的例子......
    },
    {
        tanggal:2013年7月23日,
        judul:第二条
        kutipan:文章引用的例子......
    },
    {
        tanggal:2013年7月22日,
        judul:第三条
        kutipan:文章引用的例子......
    },
    {
        tanggal:2013年3月16日,
        judul:第四条
        kutipan:Exampleofarticlequote,...
    }
]
}

OK,现在你正在与网络有关的例外是,解决的办法是,你已经使用演示code像

  @覆盖
保护无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_main);
    新NetworkThread()执行();
}类NetworkThread扩展的AsyncTask<太虚,太虚,太虚> {
    @覆盖
    保护无效doInBackground(虚空......空隙){        ArrayList的<&HashMap的LT;字符串,字符串>> artikelList =新的ArrayList<&HashMap的LT;字符串,字符串>>();
        JSONParser jParser =新JSONParser();
        JSONObject的JSON = jParser.getJSONFromUrl(ArtikelURL);        尝试{
            JSONArray artikels = json.getJSONArray(ARTIKEL);            的for(int i = 0; I< artikels.length();我++){
                JSONObject的C = artikels.getJSONObject(I)                //存储在变量中的每个JSON项目
                字符串tanggal = c.getString(TAG_TGL);
                字符串judul = c.getString(TAG_JUDUL);
                字符串kutipan = c.getString(TAG_KUTIPAN);                HashMap的<字符串,字符串>地图=新的HashMap<字符串,字符串>();
                map.put(TAG_TGL,tanggal);
                map.put(TAG_JUDUL,judul);
                map.put(TAG_KUTIPAN,kutipan);
                artikelList.add(地图);
            }
        }赶上(JSONException E){
            e.printStackTrace();
        }
        返回null;
    }    @覆盖
    保护无效onPostExecute(虚空避免){
        super.onPostExecute(避免);    }
}

不要忘了将它添加到AndroidManifest.xml文件:

 <使用许可权的android:NAME =android.permission.INTERNET对/>

I'm using JSONParser.java to show JSON data from a url

the url and the data are correct, they show some values of JSON (more than one)

but why this show only one value?

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();
        }

This is my JSON data:

{
   "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,..."
      }
   ]
}

解决方案

Your JSON is invalid, there's an error in your line 22 at 3rd object of your array

"judul":"Article Four"",

Correct JSON is

{
"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,..."
    }
]
}

Ok, Now you are getting Network related exception so, solution to this is, you've to use demo code like

@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);

    }
}

Don't forget to add this to AndroidManifest.xml file:

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

这篇关于JSON阵列不显示所有值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆