我的Android应用程序期望使用以下Java从我的WCF服务中获取用户名数组,然后填充微调器.
JSONArray mtUsers = new JSONArray(new String(buffer));
Run Code Online (Sandbox Code Playgroud)
但它似乎得到一个JSONObject,因为我得到以下错误,
Android JSONObject cannot be converted to JSONArray
Run Code Online (Sandbox Code Playgroud)
返回的JSON看起来像这样,
{"GetUserNamesResult":[{"UserName":"Peyton"},{"UserName":"Drew"},{"UserName":"Brett"}]}
Run Code Online (Sandbox Code Playgroud)
这是我服务中的代码,
接口:
[OperationContract]
[WebInvoke(Method = "GET",
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Wrapped,
UriTemplate = "GetUserNames")]
IList<UserNames> GetUserNames();
Run Code Online (Sandbox Code Playgroud)
和班级:
public IList<UserNames> GetUserNames()
{
IList<UserNames> lstusernames = new List<UserNames>();
var usernames = from p in _db.Users
select p;
foreach (User singleUsernames in usernames)
{
UserNames a = new UserNames();
a.UserName = singleUsernames.UserName;
lstusernames.Add(a);
}
return lstusernames;
}
Run Code Online (Sandbox Code Playgroud)
JSON应该是什么样子,谁能看到我做错了什么?
我没想到会在黑暗中磕磕绊绊地试图返回字符串或字符串[],我想我会问你们和gals.
干杯,
麦克风.
实际上你得到jsonObject,第一个键"GetUserNamesResult"的值是JSONArray.
这样做..
JSONObject jsonResponse = new JSONObject(new String(buffer));
JSONArray mtUsers = jsonResponse.getJSONArray("GetUserNamesResult");
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
12947 次 |
| 最近记录: |