简体   繁体   English

Java和Jersey使用类别ID作为键的类别的JSON反序列化

[英]JSON de-serialization of categories with categoryID as key with Java and Jersey

We are consuming a REST webservice (Shopware) to fetch product information. 我们正在使用REST Web服务(Shopware)来获取产品信息。 Our problem are the categories which are always null. 我们的问题是类别始终为空。

The json which comes over the wire looks like this: 电线上的json看起来像这样:

{
   "data":{
      "id":3,
      "mainDetail":{
         "id":3,
         "articleId":3,
         "categories":{
            "14":{
               "id":14,
               "name":"Edelbr\u00e4nde"
            },
            "21":{
               "id":21,
               "name":"Produktvergleiche & Filter"
            },
            "50":{
               "id":50,
               "name":"Brandies"
            }
         },
         "success":true
      }
   }
}

Our Articles class is defined like this: 我们的Articles类的定义如下:

@XmlElement(name = "categories")
public Category[] getCategories() {
       return categories;
}

public void setCategories(Category[] categories) {

       this.categories = categories;
}



    Articles articles = apiClient.getClient().resource(apiClient.getBaseUrl() + this.getResourcePath()).queryParams(this.params).type(MediaType.APPLICATION_JSON).accept(MediaType.APPLICATION_JSON).get(Articles.class);

// this always returns null
Categories categories = articles.getCategories();  

We are able to access all fields without problems except the categories. 我们可以访问所有字段,而类别除外。 We assume it is because the ids which are the keys so that Jersey does not now how to map them to the correct class. 我们假设这是因为id是键,所以Jersey现在不再如何将它们映射到正确的类。

Questions: 问题:

  1. Is there a name for this kind of json notation of the categories so that I have something to search in google? 是否有此类类别的json表示法的名称,这样我就可以在Google中进行搜索了?

  2. Do you have any hints on jersey annotations, custom type-adapters or something like that which tells the de-serializer to recognize the categories and populate our categories collection? 您是否有关于球衣注解,自定义类型适配器或类似的内容的提示,这些内容告诉反序列化器识别类别并填充类别集合?

Reposting the comment of Brian Roach as answer: 重新发布Brian Roach的评论作为答案:

since 'categories' in your JSON isn't an array (its an object), its unlikely its going to map to an array in java. 由于JSON中的“类别”不是数组(它是对象),因此不太可能映射到Java中的数组。 Change it to: 更改为:

Map<String,Category>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM