我将一组 json 结果回显给 android:
$result = mysql_query($query) or die(mysql_error());
$resultNo = mysql_num_rows($result);
// check for successful store
if ($result != null) {
$rows = array();
while($r = mysql_fetch_assoc($result)) {
$rows[] = $r;
}
return json_encode($rows);
} else {
return false;
}
}
但是当我尝试在另一端将字符串转换为 JSONObject 时,我得到:
11-13 22:18:41.990: E/JSON(5330): "[{\"email\":\"fish\"}]"
11-13 22:18:41.990: E/JSON Parser(5330): Error parsing data org.json.JSONException: Value [{"email":"fish"}] of type java.lang.String cannot be converted to JSONObject
我已经用更大的结果集尝试了这个,并认为这与空值有关,但是像上面那样尝试只使用一个值仍然会返回错误。
非常感谢任何帮助
编辑:
安卓方法...
public JSONObject searchPeople(String tower) {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("tag", search_tag));
params.add(new BasicNameValuePair("tower", tower));
// getting JSON Object
JSONObject json = jsonParser.getJSONFromUrl(loginURL, params);
// return json
return json;
}
JSON解析器类...
public JSONObject getJSONFromUrl(String url, List<NameValuePair> params) {
// Making HTTP request
try {
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(params));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException 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;
}