0

解析json时出现异常

{
    "LIVE": {
        "CONTENTS": [
    {
                    "UID":"488b93abb60",
                    "NAME":"career will be in 2013",
                    "SHORTDSEC":"How Salman Shahrukhs career will be in 2013 Deepika Ranbir wish fans a Happy New Year more",
                    "SYNOPSIS":"",
                    "TITLE":"How Salman Shahrukhs career will be in 2013",
                    "SMALLIMAGE":"in_Traffic_PoliceBKBKM61.jpg",
                    "BIGIMAGE":"in_Traffic_PoliceBKBKM154.jpg",
                    "URL2G":"h_fans_a_Happy_New_Year_more.3gp",
                    "URL3G":"sh_fans_a_Happy_New_Year_more_3g.mp4"
                },
    {
                    "UID":"5b72b81cc632",
                    "NAME":"Is Mumbai police biased towards Salman",
                    "SHORTDSEC":"Is Mumbai police biased towards Salman Top 10 Bollywood controversies of 2012 more news",
                    "SYNOPSIS":"",
                    "TITLE":"Is Mumbai police biased towards Salman",
                    "SMALLIMAGE":"_police_biased_towards_Salman61.jpg",
                    "BIGIMAGE":"i_police_biased_towards_Salman154.jpg",
                    "URL2G":"10_Bollywood_controversies_of_2012_more_news.3gp",
                    "URL3G":"_Bollywood_controversies_of_2012_more_news_3g.mp4"
                },
                {
                    "UID":"5488b93abb60",
                    "NAME":"How Salman Shahrukhs career will be in 2013",
                    "SHORTDSEC":"How Salman Shahrukhs career will be in 2013 Deepika Ranbir wish fans a Happy New Year more",
                    "SYNOPSIS":"",
                    "TITLE":"How Salman Shahrukhs career will be in 2013",
                    "SMALLIMAGE":"be_in_Traffic_PoliceBKBKM61.jpg",
                    "BIGIMAGE":"raffic_PoliceBKBKM154.jpg",
                    "URL2G":"ans_a_Happy_New_Year_more.3gp",
                    "URL3G":"r_wish_fans_a_Happy_New_Year_more_3g.mp4"
                },

            ]
        }
    }

源代码

public class GetData extends AsyncTask<String, Void, String> {
        private ProgressDialog progress = null;

        @Override
        protected String doInBackground(String... params) {
            return getJSONString(params[0]);

        }

        private String getJSONString(String url) {
            String jsonString = null;
            HttpURLConnection linkConnection = null;
            try {
                URL linkurl = new URL(url);
                linkConnection = (HttpURLConnection) linkurl.openConnection();
                int responseCode = linkConnection.getResponseCode();
                if (responseCode == HttpURLConnection.HTTP_OK) {
                    InputStream linkinStream = linkConnection.getInputStream();
                    ByteArrayOutputStream baos = new ByteArrayOutputStream();
                    int j = 0;
                    while ((j = linkinStream.read()) != -1) {
                        baos.write(j);
                    }
                    byte[] data = baos.toByteArray();
                    jsonString = new String(data);
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (linkConnection != null) {
                    linkConnection.disconnect();
                }
            }
            return jsonString;
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            progress = ProgressDialog.show(MainActivity.this, "", "Loading...");
        }

        @Override
        protected void onPostExecute(String result) {
            super.onPostExecute(result);
            if (null != progress && progress.isShowing()) {
                progress.dismiss();
            }
            if (null == result || result.length() == 0) {
                Toast.makeText(MainActivity.this, "No data found from web!!!",
                        Toast.LENGTH_SHORT).show();
                MainActivity.this.finish();
            } else {
                try {
                JSONObject mainJson = new JSONObject(result);
                JSONObject jsonObject = mainJson.getJSONObject(OBJECT_NAME);
                JSONArray jsonArray = jsonObject.getJSONArray(ARRAY_NAME);
                for (int i = 0; i < jsonArray.length(); i++) {
                    JSONObject objJson = jsonArray.getJSONObject(i);
                    Log.d("UID=", objJson.getString(UID));
                    Log.d("NAME=", objJson.getString(NAME));
                    Log.d("SHORTDESC=", objJson.getString(SHORTDSEC));
                    Log.d("TITLE=", objJson.getString(TITLE));
                    Log.d("SMALLIMAGE=", objJson.getString(SMALLIMAGE));
                    Log.d("BIGIMAGE=", objJson.getString("BIGIMAGE"));
                    Log.d("URL2G=", objJson.getString(URL2G));
                    Log.d("URL3G=", objJson.getString(URL3G));
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            }
        }
    }

异常,日志猫

 org.json.JSONException: Value at 8 is null.
W/System.err(  833):    at org.json.JSONArray.get(JSONArray.java:259)
W/System.err(  833):    at org.json.JSONArray.getJSONObject(JSONArray.java:480)
W/System.err(  833):    at com.example.chhotatv.MainActivity$GetData.onPostExecute(MainActivity.java:99)
W/System.err(  833):    at com.example.chhotatv.MainActivity$GetData.onPostExecute(MainActivity.java:1)
W/System.err(  833):    at android.os.AsyncTask.finish(AsyncTask.java:417)
W/System.err(  833):    at android.os.AsyncTask.access$300(AsyncTask.java:127)
W/System.err(  833):    at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429)
W/System.err(  833):    at android.os.Handler.dispatchMessage(Handler.java:99)
W/System.err(  833):    at android.os.Looper.loop(Looper.java:123)
W/System.err(  833):    at android.app.ActivityThread.main(ActivityThread.java:4627)
W/System.err(  833):    at java.lang.reflect.Method.invokeNative(Native Method)
W/System.err(  833):    at java.lang.reflect.Method.invoke(Method.java:521)
W/System.err(  833):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
W/System.err(  833):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
W/System.err(  833):    at dalvik.system.NativeStart.main(Native Method)
4

2 回答 2

2

CONTENTSJSONArray里面,LIVE JSONObject而不是您尝试使用的 String getString("CONTENTS");。从主 jsonobject 获取 JSONArray:

JSONObject mainJson = new JSONObject(result);
 // get LIVE JSONObject
JSONObject json_live= mainJson.getJSONObject("LIVE");
// get CONTENTS JSONArray from json_live
JSONArray json_contents= json_live.getJSONArray("CONTENTS");
于 2013-06-25T08:45:31.173 回答
0

为什么要采取 // 获取 LIVE JSONObject JSONObject json_live= mainJson.getJSONObject("LIVE"); /*没有必要 */

您可以直接访问

JSONObject mainJson = new JSONObject(result);

// 从 json_live 获取 CONTENTS JSONArray

JSONArray json_contents= mainJson.getJSONArray("CONTENTS");

于 2014-01-12T18:24:05.753 回答