ds0678 2012-05-23 14:06
浏览 111
已采纳

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

I'm having a problem parsing the JSON response from MySQL in Java.

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);

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:

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
$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();
?>
  • 写回答

1条回答 默认 最新

  • doutang7383 2012-05-23 14:13
    关注

    Your JSON data 2[{"longtitude":"32.32","latitude":"33.12"}] isn't valid (the number 2 isn't proper JSON syntax).

    Can I suggest you actually mean

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

    (ie. without the 2 at the beginning)

    You can use the validator at http://jsonlint.com/ to check your JSON code.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥15 统计大规模图中的完全子图问题
  • ¥15 使用LM2596制作降压电路,一个能运行,一个不能
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路
  • ¥15 phython读取excel表格报错 ^7个 SyntaxError: invalid syntax 语句报错
  • ¥20 @microsoft/fetch-event-source 流式响应问题
  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式