get JSONException:解析JSON响应时,无法将java.lang.String类型的值转换为JSONObject(get JSONException: Value of type ja

编程入门 行业动态 更新时间:2024-10-23 17:32:43
get JSONException:解析JSON响应时,无法将java.lang.String类型的值转换为JSONObject(get JSONException: Value of type java.lang.String cannot be converted to JSONObject when parsing a JSON response)

我开发了一个Android应用程序,它请求服务器的地点坐标以JSON格式响应(目前它只发送两个地方):

这是来自服务器的php代码:

$place = $db->getCoordinates($name); if ($place != false) { $response[1]["success"] = 1; $response[1]["place"]["H"] = $place[1]["H"]; $response[1]["place"]["V"] = $place[1]["V"]; $response[1]["place"]["placeid"] = $place[1]["placeid"]; $response[1]["place"]["name"] = $place[1]["name"]; $response[1]["place"]["type"] = $place[1]["type"]; $response[1]["place"]["note"] = $place[1]["note"]; // place found // echo json with success = 1 $response[2]["success"] = 1; $response[2]["place"]["H"] = $place[2]["H"]; $response[2]["place"]["V"] = $place[2]["V"]; $response[2]["place"]["placeid"] = $place[2]["placeid"]; $response[2]["place"]["name"] = $place[2]["name"]; $response[2]["place"]["type"] = $place[2]["type"]; $response[2]["place"]["note"] = $place[2]["note"]; echo json_encode($response); }

当应用获取坐标时,它会尝试以这种方式解析它们:

JSONObject json_places = userFunction.getPlaces(); JSONObject places = json_places.getJSONObject("1"); JSONObject coord = places.getJSONObject("place");

getplaces():

public JSONObject getPlaces(){ // Building Parameters List<NameValuePair> params = new ArrayList<NameValuePair>(); params.add(new BasicNameValuePair("tag", allcoordinates_tag)); JSONObject json = jsonParser.getJSONFromUrl("http://"+ip+placesURL, params); // return json Log.i("JSON", json.toString()); return json; }

JSONParser:

public class JSONParser { static InputStream is = null; static JSONObject jObj = null; static String json = ""; // constructor public JSONParser() { } public JSONObject getJSONFromUrl(String url, List<NameValuePair> params) { // Making HTTP request try { // defaultHttpClient HttpPost httpPost = new HttpPost(url); HttpParams httpParameters = new BasicHttpParams(); int timeoutConnection = 9000; HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection); int timeoutSocket = 9000; HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket); DefaultHttpClient httpClient = new DefaultHttpClient(httpParameters); httpPost.setEntity(new UrlEncodedFormEntity(params)); HttpResponse httpResponse = httpClient.execute(httpPost); HttpEntity httpEntity = httpResponse.getEntity(); is = httpEntity.getContent(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (ConnectTimeoutException e) { e.printStackTrace(); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (Exception 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(); Log.e("JSON", json); } 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 jObj; } }

这是带有错误的JSON:

当App试图解析JSON响应时,它会崩溃并发送一个JSONException:java.lang.String类型的值无法转换为JSONObject我该如何解决这个问题? 谢谢

I have developed an Android application that requests places coordinates from a server which responds in JSON format(for the moment it just sends two places):

this is the php code from the server:

$place = $db->getCoordinates($name); if ($place != false) { $response[1]["success"] = 1; $response[1]["place"]["H"] = $place[1]["H"]; $response[1]["place"]["V"] = $place[1]["V"]; $response[1]["place"]["placeid"] = $place[1]["placeid"]; $response[1]["place"]["name"] = $place[1]["name"]; $response[1]["place"]["type"] = $place[1]["type"]; $response[1]["place"]["note"] = $place[1]["note"]; // place found // echo json with success = 1 $response[2]["success"] = 1; $response[2]["place"]["H"] = $place[2]["H"]; $response[2]["place"]["V"] = $place[2]["V"]; $response[2]["place"]["placeid"] = $place[2]["placeid"]; $response[2]["place"]["name"] = $place[2]["name"]; $response[2]["place"]["type"] = $place[2]["type"]; $response[2]["place"]["note"] = $place[2]["note"]; echo json_encode($response); }

When the app get the coordinates it try to parse them this way:

JSONObject json_places = userFunction.getPlaces(); JSONObject places = json_places.getJSONObject("1"); JSONObject coord = places.getJSONObject("place");

getplaces():

public JSONObject getPlaces(){ // Building Parameters List<NameValuePair> params = new ArrayList<NameValuePair>(); params.add(new BasicNameValuePair("tag", allcoordinates_tag)); JSONObject json = jsonParser.getJSONFromUrl("http://"+ip+placesURL, params); // return json Log.i("JSON", json.toString()); return json; }

JSONParser:

public class JSONParser { static InputStream is = null; static JSONObject jObj = null; static String json = ""; // constructor public JSONParser() { } public JSONObject getJSONFromUrl(String url, List<NameValuePair> params) { // Making HTTP request try { // defaultHttpClient HttpPost httpPost = new HttpPost(url); HttpParams httpParameters = new BasicHttpParams(); int timeoutConnection = 9000; HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection); int timeoutSocket = 9000; HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket); DefaultHttpClient httpClient = new DefaultHttpClient(httpParameters); httpPost.setEntity(new UrlEncodedFormEntity(params)); HttpResponse httpResponse = httpClient.execute(httpPost); HttpEntity httpEntity = httpResponse.getEntity(); is = httpEntity.getContent(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (ConnectTimeoutException e) { e.printStackTrace(); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (Exception 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(); Log.e("JSON", json); } 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 jObj; } }

and here is the JSON with the error:

when the App tries to parse the JSON response it crashes and sends a JSONException: Value of type java.lang.String cannot be converted to JSONObject how can i solve this? THANKS

最满意答案

您的Web服务未创建有效的JSON。 JSON字符串只能以{或[ 。 你的字符串"Array" 。

您可以在此处阅读其Wikipedia条目中的JSON格式。

You web service is not creating valid JSON. A JSON string can only start with { or [. Yours starts with the String "Array".

You can read about the JSON format in its Wikipedia entry here.

更多推荐

本文发布于:2023-07-29 20:38:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1319566.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:转换为   类型   lang   String   JSONException

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!