JSONException:解析时无法将 java.lang.String 类型的值转换为 JSONObject
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22057553/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JSONException: Value of type java.lang.String cannot be converted to JSONObject when parsing
提问by user2574427
So I get that error when I try to parse the JSON Object. I have google the problem and I am still confused and can't find a solution to it. It looks like my code is correct.
因此,当我尝试解析 JSON 对象时出现该错误。我已经谷歌了这个问题,但我仍然很困惑,找不到解决办法。看起来我的代码是正确的。
This is the Android Java file
这是 Android Java 文件
InputStream is = null;
String result = "";
protected String doInBackground(String... params) {
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(params[0]);
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
Log.v("mysql", "Success");
} catch (Exception e) {
Log.v("log_tag", "Error in http connection " + e.toString());
}
return null;
}
protected void onPostExecute(String result) {
try{
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"UTF-8"));
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result=sb.toString();
Log.v("result", result);
}catch(Exception e){
Log.e("log_tag", "Error converting result "+e.toString());
}
try{
JSONArray jArray = new JSONArray(result);
for(int i=0;i<jArray.length();i++){
JSONObject json_data = jArray.getJSONObject(i);
Log.i("log_tag","uID: "+json_data.getInt("uID")+
", uName: "+json_data.getString("uName")+
", uPass: "+json_data.getString("uPass")
);
}
}catch(JSONException e){
Log.e("log_tag", "Error parsing data "+e.toString());
}
}
The code for PHP file is this
PHP文件的代码是这样的
<?php
$username = "removed";
$password = "removed";
$database = "removed";
$user = "art";
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die ("it's dead jim");
$q = mysql_query("SELECT * FROM user WHERE uName = '$user'");
while($e=mysql_fetch_assoc($q)){
print(json_encode($e));
$output[]=$e;
}
?>
The JSON value I receive is
我收到的 JSON 值是
{"uID":"1","uName":"art","uPass":"password"}
{"uID":"1","uName":"art","uPass":"密码"}
When I try to parse it I get:
当我尝试解析它时,我得到:
Error parsing data org.json.JSONException: Value {"uID":"1","uName":"art","uPass":"password"} of type org.json.JSONObject cannot be converted to JSONArray
解析数据 org.json.JSONException 时出错:无法将 org.json.JSONObject 类型的值 {"uID":"1","uName":"art","uPass":"password"} 转换为 JSONArray
回答by Sushil
You data in the file is in JSONObject format and you are tryong to receive it in JSONArray format which is causing the problem. The line below is the culprit:
您文件中的数据采用 JSONObject 格式,您正在尝试以 JSONArray 格式接收它,这导致了问题。下面的行是罪魁祸首:
JSONArray jArray = new JSONArray(result);
Rather, you need to do something like this:
相反,你需要做这样的事情:
JSONObject jObject = null;
jObject = new JSONObject(mJsonString);
JSONArray jsonImageArray = jObject.getJSONArray("imageTarget");
Hope it helps.
希望能帮助到你。