的JSONObject不能转换为JSONArray [英] JSONObject cannot be converted to JSONArray

查看:332
本文介绍了的JSONObject不能转换为JSONArray的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Andr​​oid应用程序期待,并使用下面的Java,以再填充微调从我的WCF服务的用户名的数组。

My Android application is expecting and array of usernames from my WCF Service using the following Java, to then populate a spinner.

JSONArray mtUsers = new JSONArray(new String(buffer));

不过,这似乎是得到一个JSONObject的,因为我碰到下面的错误,

But it seems to be getting a JSONObject as I get the following error,

Android JSONObject cannot be converted to JSONArray

所返回的JSON看起来像这样,

The JSON that’s returned looks like this,

{"GetUserNamesResult":[{"UserName":"Peyton"},{"UserName":"Drew"},{"UserName":"Brett"}]}

这是在code。在我的服务,

This is the code in my service,

接口:

[OperationContract]
[WebInvoke(Method = "GET",
    ResponseFormat = WebMessageFormat.Json,
    BodyStyle = WebMessageBodyStyle.Wrapped,
    UriTemplate = "GetUserNames")]
IList<UserNames> GetUserNames();

和类:

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;
}

我应该JSON的样子和任何人都可以看到我在做什么错了???

What should the JSON look like and can anyone see what I’m doing wrong???

而不是绊脚石左右在黑暗中试图返回一个字符串或字符串[],我想我会问你们和三角旗号。

Instead of stumbling around in the dark trying to return a string or string[], I thought I would ask you guys and gals.

干杯,

麦克。

推荐答案

实际上你得到的JSONObject,第一个键GetUserNamesResult的值是JSONArray。

actually you are getting jsonObject, the value of the first key "GetUserNamesResult" is JSONArray.

这样做..

JSONObject jsonResponse = new JSONObject(new String(buffer));
JSONArray mtUsers = jsonResponse.getJSONArray("GetUserNamesResult");

这篇关于的JSONObject不能转换为JSONArray的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆