【问题标题】:Jackson: Convert JSON to object: ArrayList of objects with arraylist of objects with arraylist of objectsJackson:将 JSON 转换为对象:ArrayList of objects with arraylist of objects with arraylist of objects
【发布时间】:2015-02-13 09:42:07
【问题描述】:

我有一个调用 do rest api 的 android 应用程序。 它返回餐馆列表。每家餐厅都有类别列表。每个类别可以有多个菜肴。

当我反序列化这个列表时,我有一个不错的餐厅数组列表,每个餐厅也有一个类别数组列表,但是在类别类中,菜肴数组列表为空。

有人知道如何解决这个问题吗?

JSON 响应:(实际上是一个 ServiceResponse 类型的对象)

{
 "requestToken" : 0,
  "responseCode" : 2,
  "objectType" : "com.test.Restaurant",
  "error" : null,
 "data" : [ {
    "id" : 1,
    "name" : "Name",
    "addressCity" : "Warszawa",
    "addressStreet" : "Kopernika",
    "addressStreetNumber" : "4",
    "addressApartmentNumber" : "17",
    "categories" : [ {
      "id" : 1,
      "restaurantId" : 1,
      "name" : "Breakfast",
      "description" : "Description",
      "dishes" : [ {
        "id" : 11,
        "categoryId" : 1,
        "name" : "Dish 1",
        "description" : "Descr",
        "price" : 8.99,
        "photoId" : 51
      }, {
        "id" : 21,
        "categoryId" : 1,
        "name" : "Dish 2",
        "description" : "Descr",
        "price" : 6.99,
        "photoId" : 41
      }, {
        "id" : 31,
        "categoryId" : 1,
        "name" : "Dish 3",
        "description" : "Descr",
        "price" : 14.99,
        "photoId" : 61
      } ]
    }, {
      "id" : 2,
      "restaurantId" : 1,
      "name" : "Obiady",
      "description" : "Opis kategorii",
      "dishes" : [ {
        "id" : 41,
        "categoryId" : 2,
        "name" : "Danie 4",
        "description" : "Descr",
        "price" : 6.99,
        "photoId" : 0
      }, {
        "id" : 61,
        "categoryId" : 2,
        "name" : "Danie 5",
        "description" : "Descr",
        "price" : 8.0,
        "photoId" : 0
      }, {
        "id" : 101,
        "categoryId" : 2,
        "name" : "Nowy obiad",
        "description" : "",
        "price" : 111.0,
        "photoId" : 0
      } ]
    }, {
      "id" : 3,
      "restaurantId" : 1,
      "name" : "Pasta",
      "description" : "Opis kategorii",
      "dishes" : [ ]
    }, {
      "id" : 4,
      "restaurantId" : 1,
      "name" : "Przystawki",
      "description" : "Opis kategorii",
     "dishes" : [ ]
    }, {
      "id" : 11,
      "restaurantId" : 1,
      "name" : "Some category",
      "description" : "test",
      "dishes" : [ ]
    }, {
      "id" : 51,
      "restaurantId" : 1,
      "name" : "Sushi",
      "description" : "",
      "dishes" : [ ]
    } ]
  }]
  }

用 Jackson 解析结果:

ServiceResponse response = getServiceOutput(params, httpMethod);

if(response.getData() != null && response.getData().size() > 0) {
    try {
        // convert data array from object to class defined in objectType (in JSON)
        ObjectMapper mapper = new ObjectMapper();
        Class clazz = Class.forName( response.getObjectType() );
        ArrayList list = (mapper.convertValue(response.getData(), 
            TypeFactory.defaultInstance().constructCollectionType(
                    ArrayList.class, clazz)));

        broadcastIntent.putExtra("result", list);
        broadcastIntent.putExtra("response_code", ServiceResponse.OK);
    } catch (Exception e) {
        broadcastIntent.putExtra("response_code", ServiceResponse.ERROR);
        e.printStackTrace();
    }
}

【问题讨论】:

  • 您能否提供对象层次结构/您正在使用的类的代码(尤其是Category.java
  • 好吧。我认为这很简单。餐厅有 ArrayList 类别; Categories 有 ArrayList 菜品;你问的是这个吗?
  • 是的,希望变量、getter/setter 或注释中的简单错字导致问题,因为其他所有类都已正确解析

标签: java android json jackson


【解决方案1】:

这对我有用。我将类(响应、餐厅等)声明为静态的原因是因为 Jackson 无法反序列化非静态内部类。

 public class Test {

static String jsonString = "{ \"requestToken\":0, \"responseCode\":2, \"objectType\":\"com.test.Restaurant\", \"error\":null, \"data\":[ { \"id\":1, \"name\":\"Name\", \"addressCity\":\"Warszawa\", \"addressStreet\":\"Kopernika\", \"addressStreetNumber\":\"4\", \"addressApartmentNumber\":\"17\", \"categories\":[ { \"id\":1, \"restaurantId\":1, \"name\":\"Breakfast\", \"description\":\"Description\", \"dishes\":[ { \"id\":11, \"categoryId\":1, \"name\":\"Dish 1\", \"description\":\"Descr\", \"price\":8.99, \"photoId\":51 }, { \"id\":21, \"categoryId\":1, \"name\":\"Dish 2\", \"description\":\"Descr\", \"price\":6.99, \"photoId\":41 }, { \"id\":31, \"categoryId\":1, \"name\":\"Dish 3\", \"description\":\"Descr\", \"price\":14.99, \"photoId\":61 } ] }, { \"id\":2, \"restaurantId\":1, \"name\":\"Obiady\", \"description\":\"Opis kategorii\", \"dishes\":[ { \"id\":41, \"categoryId\":2, \"name\":\"Danie 4\", \"description\":\"Descr\", \"price\":6.99, \"photoId\":0 }, { \"id\":61, \"categoryId\":2, \"name\":\"Danie 5\", \"description\":\"Descr\", \"price\":8.0, \"photoId\":0 }, { \"id\":101, \"categoryId\":2, \"name\":\"Nowy obiad\", \"description\":\"\", \"price\":111.0, \"photoId\":0 } ] }, { \"id\":3, \"restaurantId\":1, \"name\":\"Pasta\", \"description\":\"Opis kategorii\", \"dishes\":[ ] }, { \"id\":4, \"restaurantId\":1, \"name\":\"Przystawki\", \"description\":\"Opis kategorii\", \"dishes\":[ ] }, { \"id\":11, \"restaurantId\":1, \"name\":\"Some category\", \"description\":\"test\", \"dishes\":[ ] }, { \"id\":51, \"restaurantId\":1, \"name\":\"Sushi\", \"description\":\"\", \"dishes\":[ ] } ] } ] }";

public static void main(String[] args) throws Exception {
    ObjectMapper mapper = new ObjectMapper();
    mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    Response res = mapper.readValue(jsonString, Response.class);
}

public static class Response {
    public String requestToken;
    public List<Restaurant> data;

    public Response(){}

    public String getRequestToken() {
        return requestToken;
    }

    public void setRequestToken(String requestToken) {
        this.requestToken = requestToken;
    }

    public List<Restaurant> getData() {
        return data;
    }

    public void setData(List<Restaurant> data) {
        this.data = data;
    }
}

public static class Restaurant {
    public List<Category> categories;

    public List<Category> getCat() {
        return categories;
    }

    public void setCat(List<Category> categories) {
        this.categories = categories;
    }

    public Restaurant(){}
}

public static class Category {
    public List<Dish> dishes;

    public Category(){}

    public List<Dish> getDishes() {
        return dishes;
    }

    public void setDishes(List<Dish> dishes) {
        this.dishes = dishes;
    }
}

public static class Dish {

    public String id, categoryId, name, description;
    public float price;
    public int photoId;

    public Dish(){}

}
}

【讨论】:

  • 我已经添加了,但还是不行。
  • 请用功能性源代码查看我更新的答案。
  • 有没有办法在 Response 类中使用 List 做到这一点?这样我就可以把所有的东西都放在那里,而不仅仅是餐厅?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-09-16
  • 2013-03-28
  • 1970-01-01
  • 2019-03-15
  • 2016-11-27
  • 2014-07-21
  • 2015-03-27
相关资源
最近更新 更多