Android 不使用Arrayname解析Json

Android 不使用Arrayname解析Json,android,json,Android,Json,我正在解析我的应用程序中的json数组。在这方面,我收到了错误 以下是我的活动代码: ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>(); // Creating JSON Parser instance JSONParser jParser = new JSONParser();

我正在解析我的应用程序中的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();
            }
但我收到错误。我的日志如下:

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结构。添加一个
System.out.println(json)
json=sb.toString()行之后以查看实际获得的内容。

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

 is.close();
 json = sb.toString();
 Log.i(TAG, json);  //add this log 
您可以通过Log.i查看真实的josn字符串。 可能这不是你真正的回答参见: [首先检查字符集的内容类型标题]

我相信您在电话中遇到了问题:
BufferedReader reader=新的BufferedReader(新的InputStreamReader(is,“iso-8859-1”),8

只需记录Mejonzhan提到的最后一个json字符串,以确认它


希望这有帮助。

您的请求有误,或者服务器的响应有误。因此“无法解析,谢谢Tom Dignan…我也将发布我的json响应..请参阅我编辑的帖子..谢谢Ridcully…Ridcully我已经发布了我的web服务返回的内容..那么我要做什么来解析呢???嗨,Ridcully,我得到的是html格式,请告诉我如何使用json提取数据。你能发布,你实际得到了什么?一般来说,您应该更改服务器端实现,使其返回JSON响应而不是HTML。嗨,Ridcully,我没有在服务器端更改的控制权。请发布您从服务器获得的信息,好吗?指定的
json
变量的值。如果JSON结构在那里的某个地方,您可以删除前面和后面的HTML内容。
[{"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/"}]
 is.close();
 json = sb.toString();
 Log.i(TAG, json);  //add this log