English 中文(简体)
试图从 MySQL 查询中解析 JSON 时的例外
原标题:Exception when trying to parse JSON from a MySQL query

我对JSON在爪哇的 MySQL 的反应有疑问

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() + "
");

    String line = "0";
    while ((line = bufferedReader.readLine()) != null) {
        sb.append(line + "
");
    }
    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. the

Log.d("RESULT", result);

设置正确结果的行数 :

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

然而,

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

Never gets called, so the problem seems to be on this line

JSONObject json_data = new JSONObject(result);

本文取自一个教程, 我在互联网上和这个网站上看到许多例子, 有一些指出的错误, 但不是这个错误。

Any help would be great! Thanks

EDIT: The printStackTrace() output:

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"}]`

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();
?>
最佳回答

您的 JSON 数据 2 [{{}"长度" :"32.32", "纬度" :"33.12"} 是无效的 (数字 2 是不恰当的 JSON 语法 ) 。

我能建议你的意思是

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

(在开头没有 2 的情况下)

您可以在“http://jsonlint.com/” rel=“nofollow”>上使用验证符来检查您的 JSON 代码。

问题回答

暂无回答




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签