修复类型org.json.JSONArray的值无法转换为JSONObject

时间:2014-04-04 02:02:33

标签: android json

请帮我解析一下页面!我不介意做什么

PHP代码:

[{"DATE_REG":"04.04.2014","NAME_WDAY":"\u041f`\u044f\u0442\u043d\u0438\u0446\u044f","NAME_PAIR":"3 \u043f\u0430\u0440\u0430","TIME_PAIR":"11:25-12:45""},{"DATE_REG":"04.04.2014","NAME_WDAY":"\u041f`\u044f\u0442\u043d\u0438\u0446\u044f","NAME_PAIR":"6 \u043f\u0430\u0440\u0430","TIME_PAIR":"16:35-17:55""}]

我的代码:

private class JSONParse extends AsyncTask<String, String, JSONObject> {
         private ProgressDialog pDialog;
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
             name2 = (TextView)findViewById(R.id.name);
             para2 = (TextView)findViewById(R.id.para);
             aud2 = (TextView)findViewById(R.id.aud);

} 
                @Override
                protected JSONObject doInBackground(String... args) {
                    JSONParser jParser = new JSONParser();
                    JSONObject json = jParser.getJSONFromUrl(url);
                    return json;
                }
                 @Override
                 protected void onPostExecute(JSONObject json) {
                     pDialog.dismiss();
                     try {
                            // Getting JSON Array
                         for(int i=0; i < jsonarray.length(); i++) {
                            JSONObject c = new JSONObject();
                            // Storing  JSON item in a Variable
                            String NAME_FIO = c.getString(prepod);
                            String ABBR_DISC = c.getString(para);
                            String NAME_AUD = c.getString(audit);
                            //Set JSON Data in TextView
                            name2.setText(NAME_FIO);
                            para2.setText(ABBR_DISC);
                            aud2.setText(NAME_AUD);
                         }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

2 个答案:

答案 0 :(得分:0)

您的PHP正在返回一个数组,并且您正在尝试从中解析JSONObject。将您的AsyncTask和解析器返回类型更改为JSONArray,并在onPostExecute循环中使用它。

答案 1 :(得分:0)

试试这个..

首先,您的答案中的JSON为无效,每个数组元素的都是额外的。

你的JSON可能会在下面..

[
    {
        "DATE_REG": "04.04.2014",
        "NAME_WDAY": "П`ятниця",
        "NAME_PAIR": "3 пара",
        "TIME_PAIR": "11:25-12:45"
    },
    {
        "DATE_REG": "04.04.2014",
        "NAME_WDAY": "П`ятниця",
        "NAME_PAIR": "6пара",
        "TIME_PAIR": "16: 35-17: 55"
    }
]

JSONArray 而非 JSONObject 尝试如下所示。

private class JSONParse extends AsyncTask<String, String, JSONArray> {

           @Override
            protected JSONArray doInBackground(String... args) {
                JSONParser jParser = new JSONParser();
                JSONArray json = jParser.getJSONFromUrl(url);
                return json;
            }

然后

             @Override
             protected void onPostExecute(JSONArray json) {
                 pDialog.dismiss();
                 try {
                        // Getting JSON Array
                     for(int i=0; i < json.length(); i++) {
                        JSONObject c = json.getJSONObject(i);
                        // Storing  JSON item in a Variable
                        String NAME_FIO = c.getString(prepod);
                        String ABBR_DISC = c.getString(para);
                        String NAME_AUD = c.getString(audit);
                        //Set JSON Data in TextView
                        name2.setText(NAME_FIO);
                        para2.setText(ABBR_DISC);
                        aud2.setText(NAME_AUD);
                     }
                } catch (JSONException e) {
                    e.printStackTrace();
                }