如何使用jackson注释解析jsonarray

时间:2014-11-14 15:30:10

标签: android json annotations jackson arrays

问题在于我无法解析一个简单的json数组

jsonarray的结构是:

   [{
    "id": 1,
    "name": "name_1"
   }, {
    "id": 2,
    "name": "name_2"
   }]

我尝试过以下代码:

@JsonIgnoreProperties(ignoreUnknown = true)
public class MyObject 
{
    public String id;
    public String name;
}

public class RouteServerResponse extends ArrayList<MyObject>
{
    public RouteServerResponse()
    {

    }
}

请求数据的方法是:

final JacksonRequest<RouteServerResponse> request = new JacksonRequest<RouteServerResponse>(Request.Method.GET, url, params,
      new JacksonRequestListener<RouteServerResponse>()
                {
                    @Override
                    public void onResponse(RouteServerResponse r, int statusCode, VolleyError error)
                    {
                        if (c != null)
                            c.onResponse(r, statusCode, error);
                    }

                    @Override
                    public JavaType getReturnType()
                    {
                        return SimpleType.construct(RouteServerResponse.class);
                    }
                });

我在logcat

中收到此错误
11-14 19:06:08.708  27593-28098/ru.youroute E/Volley﹕ [3991] NetworkDispatcher.run: Unhandled exception java.lang.IllegalArgumentException: Can not construct SimpleType for a Collection (class: ru.youroute.model.serverresponse.impl.RouteServerResponse)
    java.lang.IllegalArgumentException: Can not construct SimpleType for a Collection (class: ru.youroute.model.serverresponse.impl.RouteServerResponse)

1 个答案:

答案 0 :(得分:2)

Jackson's documentation

  

简单类型被定义为除识别之外的任何其他类型   容器类型(数组,集合,映射)

听起来你想要CollectionType