Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 改型-如何使用不同类型的元素解析数组?_Android_Json_Parsing_Gson_Retrofit - Fatal编程技术网

Android 改型-如何使用不同类型的元素解析数组?

Android 改型-如何使用不同类型的元素解析数组?,android,json,parsing,gson,retrofit,Android,Json,Parsing,Gson,Retrofit,我收到这个JSON字符串: { "response": [ 346, { "id": 564, "from_id": -34454802, "to_id": -34454802, "date": 1337658196, "post_type": "post" }, { "id": 2183, "from_id": -34454802,

我收到这个JSON字符串:

{
    "response": [
    346,
    {
        "id": 564,
        "from_id": -34454802,
        "to_id": -34454802,
        "date": 1337658196,
        "post_type": "post"
    },
    {
        "id": 2183,
        "from_id": -34454802,
        "to_id": -34454802,
        "date": 1423916628,
        "post_type": "post"
    },
    {
        "id": 2181,
        "from_id": -34454802,
        "to_id": -34454802,
        "date": 1423724270,
        "post_type": "post"
    }]
}
我创建了以下类:

public class Response {
    @SerializedName("response")
    ArrayList<Post> posts;
}

public class Post {
    int id;
    int from_id;
    int to_id;
    long date;
    String post_type;
}

这是因为数组的第一个元素是Number。需要哪种型号才能无误运行?

改型在默认情况下无法直接使用。这就是为什么需要自定义转换器实现的原因

这篇文章应该有助于实现

要设置转换器,只需使用:

RestAdapter getRestAdapter(...) {
    return new RestAdapter.Builder()
            ...
            .setConverter(converter)
            ...
            .build();
}

你的模型类应该是这样的,模型对象应该总是像string或ArrayList一样,带有class对象或string对象。如果你提到
int
,你会得到非法状态异常

public class Pojo
{
    private Response[] response;

    public Response[] getResponse ()
    {
        return response;
    }

    public void setResponse (Response[] response)
    {
        this.response = response;
    }
}


public class Response
{
    private String id;

    private String to_id;

    private String from_id;

    private String post_type;

    private String date;

    public String getId ()
    {
        return id;
    }

    public void setId (String id)
    {
        this.id = id;
    }

    public String getTo_id ()
    {
        return to_id;
    }

    public void setTo_id (String to_id)
    {
        this.to_id = to_id;
    }

    public String getFrom_id ()
    {
        return from_id;
    }

    public void setFrom_id (String from_id)
    {
        this.from_id = from_id;
    }

    public String getPost_type ()
    {
        return post_type;
    }

    public void setPost_type (String post_type)
    {
        this.post_type = post_type;
    }

    public String getDate ()
    {
        return date;
    }

    public void setDate (String date)
    {
        this.date = date;
    }
}

您的
JSON
响应字符串无效。您可以在此处查看如何将数字346转换为
Post
类型的对象?
public class Pojo
{
    private Response[] response;

    public Response[] getResponse ()
    {
        return response;
    }

    public void setResponse (Response[] response)
    {
        this.response = response;
    }
}


public class Response
{
    private String id;

    private String to_id;

    private String from_id;

    private String post_type;

    private String date;

    public String getId ()
    {
        return id;
    }

    public void setId (String id)
    {
        this.id = id;
    }

    public String getTo_id ()
    {
        return to_id;
    }

    public void setTo_id (String to_id)
    {
        this.to_id = to_id;
    }

    public String getFrom_id ()
    {
        return from_id;
    }

    public void setFrom_id (String from_id)
    {
        this.from_id = from_id;
    }

    public String getPost_type ()
    {
        return post_type;
    }

    public void setPost_type (String post_type)
    {
        this.post_type = post_type;
    }

    public String getDate ()
    {
        return date;
    }

    public void setDate (String date)
    {
        this.date = date;
    }
}