简体   繁体   English

尝试从MySQL查询解析JSON时出现异常

[英]Exception when trying to parse JSON from a MySQL query

I'm having a problem parsing the JSON response from MySQL in Java. 我在使用Java解析来自MySQL的JSON响应时遇到问题。

try {
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("http://parkfinder.zxq.net/default.php");
    httppost.setEntity(new UrlEncodedFormEntity(coordinatesToSend));
    HttpResponse response = httpclient.execute(httppost);
    Log.d("HTTP Client", "HTTP Request made");

    HttpEntity entity = response.getEntity();
    inputStream = entity.getContent();
    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream,
            "iso-8859-1"), 8);
    sb = new StringBuilder();
    sb.append(bufferedReader.readLine() + "\n");

    String line = "0";
    while ((line = bufferedReader.readLine()) != null) {
        sb.append(line + "\n");
    }
    inputStream.close();
    bufferedReader.close();
    result = sb.toString();
    Log.d("RESULT", result);
    JSONObject json_data = new JSONObject(result);
    Log.d("JSON","Finished");
    JSONArray nameArray = json_data.names();
    JSONArray valArray = json_data.toJSONArray(nameArray);
    for (int i = 0; i < nameArray.length(); i++) {
        Log.d("NAMES", nameArray.getString(i));
    }
    for (int i = 0; i < nameArray.length(); i++) {
        Log.d("NAMES", nameArray.getString(i));
    }

} catch (Exception e) {
    // TODO: handle exception
}

This is the MySQL Accessing and retreiving info, and parsing it afterwars. 这是MySQL访问和检索信息,并在事后解析它。 the

Log.d("RESULT", result);

line posts the correct results: 线发布正确的结果:

2[{"longtitude":"32.32","latitude":"33.12"}]

however the 但是,那

Log.d("JSON","Finished");

Never gets called, so the problem seems to be on this line 永远不会被调用,所以问题似乎就在这条线上

JSONObject json_data = new JSONObject(result);

This while thing is taken from a tutorial which I saw many examples of it over the internet and on this site, some stated errors, but not this one. 这个问题来自一个教程,我在互联网上和这个网站上看到了很多例子,一些陈述的错误,但不是这个。

Any help would be great! 任何帮助都会很棒! Thanks 谢谢

EDIT: The printStackTrace() output: 编辑:printStackTrace()输出:

0`5-14 21:38:18.639: WARN/System.err(665): org.json.JSONException: A JSONObject text must begin with '{' at character 1 of 2[{"longtitude":"32.32","latitude":"33.12"}]`

The php code: php代码:

<?php
$host = "localhost";
$user = "**MASKED**";
$password = "**MASKED**";
$database = "parkfinder_zxq_coordinates";
$connection = mysql_connect($host, $user, $password) or die("couldn't connect to server");
$db = mysql_select_db($database, $connection) or die("couldn't select database.");
//$request_parked = $_REQUEST['parked'];
$request_long = $_REQUEST['longtitude'];
$request_lat = $_REQUEST['latitude'];
//if ($request_parked == 'FIND') {
$q = mysql_query("SELECT * FROM Coordinates");
while ($e = mysql_fetch_assoc($q))
    $output[] = $e;

print (json_encode($output));
//}

mysql_close();
?>

Your JSON data 2[{"longtitude":"32.32","latitude":"33.12"}] isn't valid (the number 2 isn't proper JSON syntax). 您的JSON数据2[{"longtitude":"32.32","latitude":"33.12"}]无效(数字2不是正确的JSON语法)。

Can I suggest you actually mean 我可以建议你的意思吗?

[{"longtitude":"32.32","latitude":"33.12"}]`

(ie. without the 2 at the beginning) (即开头没有2

You can use the validator at http://jsonlint.com/ to check your JSON code. 您可以使用http://jsonlint.com/上的验证程序检查您的JSON代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM