【问题标题】:get JSONException: Value of type java.lang.String cannot be converted to JSONObject when parsing a JSON response获取 JSONException:解析 JSON 响应时,java.lang.String 类型的值无法转换为 JSONObject
【发布时间】:2013-09-13 20:14:11
【问题描述】:

我开发了一个 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;
}

JSON解析器:

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:

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

【问题讨论】:

  • 你从服务器得到的 JSON 是什么?
  • 了解调试器并查看服务器发送的内容,如前所述.....
  • JSONParser,我添加了类的
  • 什么是json,你从服务器得到的?
  • 将 json 添加到您的帖子中

标签: java android json


【解决方案1】:

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

您可以在其 Wikipedia 条目 here 中了解 JSON 格式。

【讨论】:

    【解决方案2】:

    一开始就去掉“Array”,json格式不好

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-16
      • 2015-09-19
      • 1970-01-01
      • 2018-02-22
      • 1970-01-01
      • 2015-12-25
      相关资源
      最近更新 更多