Android解析json null

时间:2014-02-13 15:26:23

标签: android json

我尝试解析json但结果为null。如果有人知道帮助我,我做错了什么 谢谢 这是我的代码

public class GetJSON extends AsyncTask<Void, Void, Void> {
    @Override
    protected Void doInBackground(Void... params) { // Running in background
        try {
            httpclient = new DefaultHttpClient(new BasicHttpParams());
            HttpPost httppost = new HttpPost(
                    "************");
            // Depends on your web service
            httppost.setHeader("Content-type", "application/json");
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();

            inputStream = entity.getContent();
            // json is UTF-8 by default
            BufferedReader reader = new BufferedReader(
                    new InputStreamReader(inputStream, "UTF-8"), 8);
            sb = new StringBuilder();

            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            result = sb.toString();
            Log.i("Jsonnnnnnnnnnnnn", result);
        }

             catch (Exception squish) {
            }


        return null;
    }

    @Override
    protected void onPreExecute() { // Activity is on progress
    }

    @Override
    protected void onPostExecute(Void v) {
        try {

            jsonArray = new JSONArray(result);


            date = new String[jsonArray.length()];

            // quote = new String[jsonArray .length()];
            // by = new String[jsonArray.length()];
            for (int i = 0; i < jsonArray .length(); i++) {
                jsonObj = (JSONObject) jsonArray .get(i);
                date[i] = jsonObj.getString("title");
                // quote[i] = jsonObj.getString("Quote");
                // by[i] = jsonObj.getString("By");
            } // End the for loop
            remoteViews.setTextViewText(R.id.widgetdesc, date[0]);

            manager.updateAppWidget(thiswidget, remoteViews);
            // remoteViews.setTextViewText(R.id.tvParkStatus, quote[0]);
        } catch (JSONException e) {
            e.printStackTrace();
            Log.e("Erorrrrrrrrrrrr", e.toString());
        }
    }
}


my json is like this

在我的代码中,我在结果字符串中收到了所有json的数组但是我无法解析我的json

2 个答案:

答案 0 :(得分:0)

结果是JSONObject而不是JSONArray。你的解析应该像:

  JSONObject jsonObject = new JSONObject(result);
  JSONArray jsonArray = jsonObject.getJSONArray("data");

答案 1 :(得分:0)

您的数据jsonArray位于jsonObject内部。

 JSONObject  jsonobject = new JSONObject(result);
 jsonArray= jsonobject.getJSONArray("data");