(Android) JSONObject 无法转换为 JSONArray
(Android) JSONObject cannot be converted to JSONArray
我下面的 android 代码正在抛出
org.json.JSONException: Value
{"ID":1,"DisplayName":"Manish","UserName":"manish.parab@hotmail.com"}
at AuthenticateUserResult of type org.json.JSONObject cannot be
converted to JSONArray
代码:
String response = Common.ExecuteHttpRequest(Url);
JSONObject jsonObject = new JSONObject(response);
JSONArray jArray = jsonObject.getJSONArray("AuthenticateUserResult");
响应是来自 WCF 方法的字符串。
{"AuthenticateUserResult":{"DisplayName":"Manish","ID":1,"UserName":"manish.parab@hotmail.com"}}
AuthenticateUserResult
的值是一个 JSON 对象(它包含在 {}
中)。
将该行更改为此
JSONObject jArray = jsonObject.getJSONObject("AuthenticateUserResult");
然后你可以得到你的数据如下:
String displayName = jArray.getString("DisplayName");
// Etc...
解决此问题的方法有 3 个。
1.Use Json对象。您的 WCF 服务器只需在 JsonObject 中提供即可。
String response = Common.ExecuteHttpRequest(Url);
JSONObject jsonObject = new JSONObject(response).getJSONObject("AuthenticateUserResult");
2.Use json 数组作为容器
String response = Common.ExecuteHttpRequest(Url);
JSONObject jsonObject = new JSONObject(response);
JSONArray jArray = new JSONArray().put(jsonObject.getJSONObject("AuthenticateUserResult"));
3.Edit 服务器提供 AuthenticationUserResult
到 json 数组。正确的格式如下。
{"AuthenticateUserResult":[{"DisplayName":"Manish","ID":1,"UserName":"manish.parab@hotmail.com"}]}
异常是正确的,因为 "AuthenticateUserResult" 值被声明为元素 ({}) 而不是数组 ({})。
要解决此问题,请使用 getJSONObject 方法获取 "AuthenticateUserResult" 的值,如下所示:
String response = Common.ExecuteHttpRequest(Url);
JSONObject jsonObject = new JSONObject(response);
JSONObject result = jsonObject.getJSONObject("AuthenticateUserResult");
之后,您可以检索子元素,例如:
String mUserName = result.getString("UserName");
我下面的 android 代码正在抛出
org.json.JSONException: Value {"ID":1,"DisplayName":"Manish","UserName":"manish.parab@hotmail.com"} at AuthenticateUserResult of type org.json.JSONObject cannot be converted to JSONArray
代码:
String response = Common.ExecuteHttpRequest(Url);
JSONObject jsonObject = new JSONObject(response);
JSONArray jArray = jsonObject.getJSONArray("AuthenticateUserResult");
响应是来自 WCF 方法的字符串。
{"AuthenticateUserResult":{"DisplayName":"Manish","ID":1,"UserName":"manish.parab@hotmail.com"}}
AuthenticateUserResult
的值是一个 JSON 对象(它包含在 {}
中)。
将该行更改为此
JSONObject jArray = jsonObject.getJSONObject("AuthenticateUserResult");
然后你可以得到你的数据如下:
String displayName = jArray.getString("DisplayName");
// Etc...
解决此问题的方法有 3 个。
1.Use Json对象。您的 WCF 服务器只需在 JsonObject 中提供即可。
String response = Common.ExecuteHttpRequest(Url);
JSONObject jsonObject = new JSONObject(response).getJSONObject("AuthenticateUserResult");
2.Use json 数组作为容器
String response = Common.ExecuteHttpRequest(Url);
JSONObject jsonObject = new JSONObject(response);
JSONArray jArray = new JSONArray().put(jsonObject.getJSONObject("AuthenticateUserResult"));
3.Edit 服务器提供 AuthenticationUserResult
到 json 数组。正确的格式如下。
{"AuthenticateUserResult":[{"DisplayName":"Manish","ID":1,"UserName":"manish.parab@hotmail.com"}]}
异常是正确的,因为 "AuthenticateUserResult" 值被声明为元素 ({}) 而不是数组 ({})。
要解决此问题,请使用 getJSONObject 方法获取 "AuthenticateUserResult" 的值,如下所示:
String response = Common.ExecuteHttpRequest(Url);
JSONObject jsonObject = new JSONObject(response);
JSONObject result = jsonObject.getJSONObject("AuthenticateUserResult");
之后,您可以检索子元素,例如:
String mUserName = result.getString("UserName");