简体   繁体   English

获取错误:解析数据 org.json.JSONException 时出错:无法将 org.json.JSONArray 类型的值转换为 JSONObject

[英]getting error :Error parsing data org.json.JSONException: Value of type org.json.JSONArray cannot be converted to JSONObject

Stuck and need help.卡住了,需要帮助。 JSON parser cannot convert data so I can use the username and password to login. JSON 解析器无法转换数据,所以我可以使用用户名和密码登录。

jsonParser = new JSONParser();
public JSONObject loginUser(String username, String password) {
    // Building Parameters
    List<NameValuePair> params = new ArrayList<NameValuePair>();
    params.add(new BasicNameValuePair("tag", login_tag));
    params.add(new BasicNameValuePair("username", username));
    params.add(new BasicNameValuePair("password", password));
    JSONObject json = jsonParser.getJSONFromUrl(loginURL, params);
    return json;
}

PHP part of the code: PHP部分代码:

while($r = mysql_fetch_assoc($result)) {
    $json[] = $r;
}
print json_encode($json);

With the comments of the @njzk2 the solution should be to change your PHP coding:根据@njzk2 的评论,解决方案应该是更改您的 PHP 编码:

But then you just get the first:但是你只会得到第一个:

while($r = mysql_fetch_assoc($result)) {
    $json = $r;
    break;
}
print json_encode($json);

Or change your Java Coding to a JsonArray或者将您的 Java 编码更改为 JsonArray

jsonParser = new JSONParser();
public JSONArray loginUser(String username, String password) {
    // Building Parameters
    List<NameValuePair> params = new ArrayList<NameValuePair>();
    params.add(new BasicNameValuePair("tag", login_tag));
    params.add(new BasicNameValuePair("username", username));
    params.add(new BasicNameValuePair("password", password));
    // change the following line, to the correct method. Which parser do you use?
    JSONArray json = jsonParser.getJSONArrayFromUrl(loginURL, params);
    return json;
}

暂无
暂无

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

相关问题 JSON解析器:解析数据org.json.JSONException时出错:类型org.json.JSONArray无法转换 - JSON Parser: Error parsing data org.json.JSONException: type org.json.JSONArray cannot be converted org.json.JSONException:值<jsonarray-here> org.json.JSONArray 类型无法转换为 JSONObject</jsonarray-here> - org.json.JSONException: Value <JSONarray-here> of type org.json.JSONArray cannot be converted to JSONObject Android 中的错误'JSONException: org.json.JSONArray 类型的值 [] 无法转换为 JSONObject' - Error 'JSONException: Value [] of type org.json.JSONArray cannot be converted to JSONObject' in Android 解析数据org.json.JSONException时出错:类型java.lang.String的值无法转换为JSONArray - Error Parsing Data org.json.JSONException: Value of type java.lang.String cannot be converted to JSONArray 解析数据org.json.JSONException时出错:值 - Error parsing data org.json.JSONException: Value <!— of type java.lang.String cannot be converted to JSONObject W/System.err: org.json.JSONException: Value [] at 0 of type org.json.JSONArray 无法转换为 JSONObject - W/System.err: org.json.JSONException: Value [] at 0 of type org.json.JSONArray cannot be converted to JSONObject 解析数据org.json.JSONException时出错:值HI。 类型java.lang.String的对象不能转换为JSONObject - Getting Error parsing data org.json.JSONException: Value HI. of type java.lang.String cannot be converted to JSONObject 错误:类型 org.json.JSONArray 无法转换为 JSONObject - error: type org.json.JSONArray cannot be converted to JSONObject 解析数据org.json.JSONException时出错:类型java.lang.String无法转换为JSONObject - Error parsing data org.json.JSONException: type java.lang.String cannot be converted to JSONObject 解析数据org.json.JSONException时出错:类型java.lang.Integer的值403无法转换为JSONArray - Error parsing data org.json.JSONException: Value 403 of type java.lang.Integer cannot be converted to JSONArray
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM