在没有Arrayname的情况下解析Json

时间:2012-11-15 07:05:16

标签: android json

我正在解析我的应用程序中的json数组。我收到错误。

以下是我的活动代码:

ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>();
         // Creating JSON Parser instance
            JSONParser jParser = new JSONParser();

            // getting JSON string from URL
//            JSONObject json = jParser.getJSONFromUrl(URL);

            try {
                JSONArray   json = new JSONArray(jParser.getJSONFromUrl(URL));


            try {
                // Getting Array of Contacts
//                contacts = json.getJSONArray(" ");

                // looping through All Contacts
                for(int i = 0; i < json.length(); i++){
                    JSONObject c = json.getJSONObject(i);

                    // Storing each json item in variable
                    String id = c.getString("name");
                    System.out.println("id ="+id );
//                    String name = c.getString(TAG_NAME);
//                    String email = c.getString(TAG_EMAIL);
//                    String address = c.getString(TAG_ADDRESS);
//                    String gender = c.getString(TAG_GENDER);
//             
//                    // Phone number is agin JSON Object
//                    JSONObject phone = c.getJSONObject(TAG_PHONE);
//                    String mobile = phone.getString(TAG_PHONE_MOBILE);
//                    String home = phone.getString(TAG_PHONE_HOME);
//                    String office = phone.getString(TAG_PHONE_OFFICE);

                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
            } catch (JSONException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }

我的解析器代码:

public class JSONParser {

    static InputStream is = null;
    static JSONObject jObj = null;
    static String json = "";

    // constructor
    public JSONParser() {

    }

    public String getJSONFromUrl(String url) {

        // Making HTTP request
        try {
            // defaultHttpClient
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(url);

            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();
            is = httpEntity.getContent();
//            is=EntityUtils.toByteArray(httpEntity);


        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    is, "iso-8859-1"), 8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            is.close();
            json = sb.toString();
        } catch (Exception e) {
            Log.e("Buffer Error", "Error converting result " + e.toString());
        }

        // try parse the string to a JSON object
        try {
            jObj = new JSONObject(json);
        } catch (JSONException e) {
            Log.e("JSON Parser", "Error parsing data " + e.toString());
        }

        // return JSON String
        return json;

    }
}

但我收到错误。我的LOGCAT如下:

11-15 12:24:46.788: E/JSON Parser(279): Error parsing data org.json.JSONException: Value <!DOCTYPE of type java.lang.String cannot be converted to JSONObject
11-15 12:24:46.788: W/System.err(279): org.json.JSONException: Value <!DOCTYPE of type java.lang.String cannot be converted to JSONArray
11-15 12:24:46.816: W/System.err(279):  at org.json.JSON.typeMismatch(JSON.java:107)
11-15 12:24:46.826: W/System.err(279):  at org.json.JSONArray.<init>(JSONArray.java:91)
11-15 12:24:46.826: W/System.err(279):  at org.json.JSONArray.<init>(JSONArray.java:103)
11-15 12:24:46.826: W/System.err(279):  at com.newspaper.MainActivity.onCreate(MainActivity.java:43)
11-15 12:24:46.826: W/System.err(279):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
11-15 12:24:46.836: W/System.err(279):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
11-15 12:24:46.845: W/System.err(279):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
11-15 12:24:46.845: W/System.err(279):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
11-15 12:24:46.856: W/System.err(279):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
11-15 12:24:46.856: W/System.err(279):  at android.os.Handler.dispatchMessage(Handler.java:99)
11-15 12:24:46.866: W/System.err(279):  at android.os.Looper.loop(Looper.java:123)
11-15 12:24:46.866: W/System.err(279):  at android.app.ActivityThread.main(ActivityThread.java:4627)
11-15 12:24:46.866: W/System.err(279):  at java.lang.reflect.Method.invokeNative(Native Method)
11-15 12:24:46.866: W/System.err(279):  at java.lang.reflect.Method.invoke(Method.java:521)
11-15 12:24:46.866: W/System.err(279):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
11-15 12:24:46.877: W/System.err(279):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
11-15 12:24:46.886: W/System.err(279):  at dalvik.system.NativeStart.main(Native Method)

我的json回复:

[{"seqNum": 1, "sectionId": 2780, "name": "Top Stories", "displayOptions": {"allowComments": false, "showOnFrontPage": true, "includeInNavBar": true, "ads": {"adPlacement": {"bottomAdSlot": true, "topAdSlot": true}}, "articlesToShow": 4}, "urlPath": "/top-stories/"}, {"seqNum": 2, "sectionId": 3044, "name": "Breaking News", "displayOptions": {"allowComments": false, "showOnFrontPage": true, "includeInNavBar": true, "ads": {"adPlacement": {"bottomAdSlot": true, "topAdSlot": true}}, "articlesToShow": 5}, "urlPath": "/breaking-news/"}, {"seqNum": 3, "sectionId": 3391, "name": "Assembly Elections 2011", "displayOptions": {"allowComments": false, "showOnFrontPage": false, "includeInNavBar": false, "ads": {"adPlacement": {"bottomAdSlot": true, "topAdSlot": true}}, "articlesToShow": 15}, "urlPath": "/assembly-elections-2011/"}, {"seqNum": 3, "sectionId": 3256, "name": "The India Cables", "displayOptions": {"allowComments": false, "showOnFrontPage": false, "includeInNavBar": true, "ads": {"adPlacement": {"bottomAdSlot": true, "topAdSlot": true}}, "articlesToShow": 5}, "urlPath": "/the-india-cables/"}, {"seqNum": 3, "sectionId": 2782, "name": "International", "displayOptions": {"includeInNavBar": true, "articlesToShow": 5, "allowComments": false, "ads": {"adPlacement": {"bottomAdSlot": true, "topAdSlot": true}}, "showOnFrontPage": false}, "urlPath": "/international/"}, {"seqNum": 3, "sectionId": 2810, "name": "States", "displayOptions": {"allowComments": false, "showOnFrontPage": false, "includeInNavBar": true, "ads": {"adPlacement": {"bottomAdSlot": true, "topAdSlot": true}}, "articlesToShow": 5}, "urlPath": "/states/"}, {"seqNum": 3, "sectionId": 2811, "name": "Cities", "displayOptions": {"allowComments": false, "showOnFrontPage": false, "includeInNavBar": true, "ads": {"adPlacement": {"bottomAdSlot": true, "topAdSlot": true}}, "articlesToShow": 5}, "urlPath": "/cities/"}, {"seqNum": 4, "sectionId": 2781, "name": "National", "displayOptions": {"includeInNavBar": true, "articlesToShow": 5, "allowComments": false, "ads": {"adPlacement": {"bottomAdSlot": true, "topAdSlot": true}}, "showOnFrontPage": true}, "urlPath": "/national/"}, {"seqNum": 4, "sectionId": 2784, "name": "Business", "displayOptions": {"allowComments": false, "showOnFrontPage": true, "includeInNavBar": true, "ads": {"adPlacement": {"bottomAdSlot": true, "topAdSlot": true}}, "articlesToShow": 5}, "urlPath": "/business/"}, {"seqNum": 4, "sectionId": 2789, "name": "Science and Technology", "displayOptions": {"includeInNavBar": true, "articlesToShow": 5, "allowComments": false, "ads": {"adPlacement": {"bottomAdSlot": true, "topAdSlot": true}}, "showOnFrontPage": false}, "urlPath": "/science-and-technology/"}, {"seqNum": 5, "sectionId": 2785, "name": "Sport", "displayOptions": {"allowComments": false, "showOnFrontPage": true, "includeInNavBar": true, "ads": {"adPlacement": {"bottomAdSlot": true, "topAdSlot": true}}, "articlesToShow": 5}, "urlPath": "/sport/"}, {"seqNum": 6, "sectionId": 2786, "name": "Life and Style", "displayOptions": {"allowComments": false, "showOnFrontPage": false, "includeInNavBar": true, "ads": {"adPlacement": {"bottomAdSlot": true, "topAdSlot": true}}, "articlesToShow": 5}, "urlPath": "/life-and-style/"}, {"seqNum": 7, "sectionId": 2787, "name": "Education", "displayOptions": {"includeInNavBar": true, "articlesToShow": 5, "allowComments": false, "ads": {"adPlacement": {"bottomAdSlot": true, "topAdSlot": true}}, "showOnFrontPage": false}, "urlPath": "/education/"}, {"seqNum": 10, "sectionId": 2790, "name": "Arts", "displayOptions": {"allowComments": false, "showOnFrontPage": false, "includeInNavBar": true, "ads": {"adPlacement": {"bottomAdSlot": true, "topAdSlot": true}}, "articlesToShow": 5}, "urlPath": "/arts/"}, {"seqNum": 10, "sectionId": 3051, "name": "Editorial", "displayOptions": {"includeInNavBar": true, "articlesToShow": 2, "allowComments": false, "ads": {"adPlacement": {"bottomAdSlot": true, "topAdSlot": true}}, "showOnFrontPage": true}, "urlPath": "/editorial/"}, {"seqNum": 11, "sectionId": 3010, "name": "Opinion", "displayOptions": {"includeInNavBar": true, "articlesToShow": 3, "allowComments": false, "ads": {"adPlacement": {"bottomAdSlot": true, "topAdSlot": true}}, "showOnFrontPage": true}, "urlPath": "/opinion/"}, {"seqNum": 12, "sectionId": 4309, "name": "MetroPlus Theatre Fest 2011", "displayOptions": {"allowComments": false, "showOnFrontPage": false, "includeInNavBar": false, "ads": {"adPlacement": {"bottomAdSlot": true, "topAdSlot": true}}, "articlesToShow": 5}, "urlPath": "/metroplus-theatre-fest-2011/"}, {"seqNum": 13, "sectionId": 2997, "name": "Slideshows", "displayOptions": {"allowComments": false, "showOnFrontPage": true, "includeInNavBar": true, "ads": {"adPlacement": {"bottomAdSlot": true, "topAdSlot": true}}, "articlesToShow": 5}, "urlPath": "/slideshows/"}]

请提前帮助我。谢谢。

3 个答案:

答案 0 :(得分:0)

看起来您没有从服务器获取JSON结构。在System.out.println(json);行之后添加json = sb.toString();以查看实际获得的内容。

答案 1 :(得分:0)

java.lang.String类型的DOCTYPE无法转换为JSONArray; 它说,响应字符串不是json格式,但像Tom说的那样,

 is.close();
 json = sb.toString();
 Log.i(TAG, json);  //add this log 

你可以通过Log.i看到真正的josn字符串。 可能不是你的真实回应

答案 2 :(得分:0)

请参阅:iso-8859-1 vs utf-8 android [首先检查charset的内容类型标题]

我相信你在这一行有一个问题:
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);

只需记录Mejonzhan提到的最终json字符串,即可确认。

希望这有帮助。