改造预期为 BEGIN_OBJECT 但为 BEGIN_ARRAY [英] Retrofit Expected BEGIN_OBJECT but was BEGIN_ARRAY

查看:49
本文介绍了改造预期为 BEGIN_OBJECT 但为 BEGIN_ARRAY的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 JSON 解析相当陌生,我正在使用 Square 的 Retrofit 库并遇到了这个问题.

我正在尝试解析这个 JSON 响应:

<预><代码>[{身份证":3,"用户名": "jezer","regid": "oiqwueoiwqueoiwqueoiwq","url": "http://192.168.63.175:3000/users/3.json"},{身份证":4,用户名":模拟器","regid": "qwoiuewqoiueoiwqueoq","url": "http://192.168.63.175:3000/users/4.json"},{身份证":7,用户名":测试","regid": "ksadqowueqiaksj","url": "http://192.168.63.175:3000/users/7.json"}]

这是我的模型:

公共类联系人{公共列表<用户>联系人;}

...

公共类用户{字符串用户名;字符串注册表;@覆盖公共字符串 toString(){返回(用户名);}}

我的界面:

公共接口 ContactsInterface {@GET("/users.json")无效联系人(回调<联系人> cb);}

我的成功方法:

@Override公共无效成功(联系人 c,响应 r){列表<字符串>名称 = 新的 ArrayList();for (int i = 0; i < c.contacts.size(); i++) {字符串名称 = c.contacts.get(i).toString();Log.d("姓名", "" + 姓名);名称.添加(名称);}ArrayAdapterspinnerAdapter = new ArrayAdapter(this,android.R.layout.simple_spinner_item,名称);mSentTo.setAdapter(spinnerAdapter);}

当我在我的成功方法中使用它时,它会抛出错误

<块引用>

预期为 BEGIN_OBJECT 但在第 1 列第 2 列处为 BEGIN_ARRAY

这里出了什么问题?

解决方案

现在您正在解析响应,就像它的格式如下:

<代码>{联系人":[{ .. }]}

异常告诉您这一点,因为您期望根中有一个对象,但实际数据实际上是一个数组.这意味着您需要将类型更改为数组.

最简单的方法是使用列表作为回调中的直接类型:

@GET("/users.json")无效联系人(回调<列表<用户>> cb);

I'm fairly new to JSON parsing, I'm using the Retrofit library of Square and ran into this problem.

I'm trying to parse this JSON reponse:

[
      {
        "id": 3,
        "username": "jezer",
        "regid": "oiqwueoiwqueoiwqueoiwq",
        "url": "http://192.168.63.175:3000/users/3.json"
      },
      {
        "id": 4,
        "username": "emulator",
        "regid": "qwoiuewqoiueoiwqueoq",
        "url": "http://192.168.63.175:3000/users/4.json"
      },
      {
        "id": 7,
        "username": "test",
        "regid": "ksadqowueqiaksj",
        "url": "http://192.168.63.175:3000/users/7.json"
      }
]

Here are my models:

public class Contacts {

    public List<User> contacts;

}

...

public class User {

    String username;
    String regid;

    @Override
    public String toString(){
        return(username);
    }  

}

my Interface:

public interface ContactsInterface {

    @GET("/users.json")
    void contacts(Callback<Contacts> cb);

}

my success method:

@Override
public void success(Contacts c, Response r) {
    List<String> names = new ArrayList<String>();
    for (int i = 0; i < c.contacts.size(); i++) {
        String name = c.contacts.get(i).toString();
        Log.d("Names", "" + name);
        names.add(name);
    }
    ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_item, names);
    mSentTo.setAdapter(spinnerAdapter);
}

When I use it on my success method it throws the error

Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column2

What is wrong here?

解决方案

Right now you are parsing the response as if it was formatted like this:

{
  "contacts": [
    { .. }
  ]
}

The exception tells you this in that you are expecting an object at the root but the real data is actually an array. This means you need to change the type to be an array.

The easiest way is to just use a list as the direct type in the callback:

@GET("/users.json")
void contacts(Callback<List<User>> cb);

这篇关于改造预期为 BEGIN_OBJECT 但为 BEGIN_ARRAY的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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