【问题标题】:Serialize JSON document using Jackson使用 Jackson 序列化 JSON 文档
【发布时间】:2013-09-14 21:32:18
【问题描述】:

我正在尝试使用 Jackson 库序列化 JSON 文档。下面是我手工创建的 JSON 文档。现在我需要使用 Jackson 序列化这个文档

示例-A

{
    "v" : {
        "site_id" : 0,
        "price_score" : 0.5,
        "confidence_score" : 0.2,
        "categories": {
            "123" : {
                "price_score": "0.5",
                "confidence_score": "0.2"
            },
            "321" : {
                "price_score": "0.2",
                "confidence_score": "0.4"
            }
        }
    }
}

到目前为止,我可以使用下面的代码并使用 Jackson 来制作 JSON 文档的这一部分-

示例-B

{
  "v" : {
    "site_id" : 0,
    "price_score" : 0.5,
    "confidence_score" : 0.2
  }
}       

现在,我无法理解如何使用以下代码在我的 Example-B JSON 文档中添加 categories(如 Example-A 中所示)部分的列表?

public static void main(String[] args) {

    Map<String, Object> props = new HashMap<String, Object>();
    props.put("site-id", 0);
    props.put("price-score", 0.5);
    props.put("confidence-score", 0.2);

    AttributeValue av = new AttributeValue();
    av.setProperties(props);

    /**
     * this will print out the JSON document like I shown in my Example-B
     * but I need to make it look like as in Example-A. I am not sure how
     * to achieve that?
     */
    System.out.println(av);

    // serialize it
    try {
        String jsonStr = JsonMapperFactory.get().writeValueAsString(attr);
        System.out.println(jsonStr);
    } catch (JsonGenerationException e) {
        e.printStackTrace();
    } catch (JsonMappingException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

}

有人可以帮我吗?

【问题讨论】:

    标签: java json serialization jackson jsonserializer


    【解决方案1】:

    解决方案 1
    在您的情况下,您可以使用

    Map<String, Object> props = new HashMap<String, Object>();
    props.put("site-id", 0);
    props.put("price-score", 0.5);
    props.put("confidence-score", 0.2);
    Map<String, String> category123 = new HashMap<String, String>();
    category123.put("price_score", "0.5");
    category123.put("confidence_score", "0.2");
    Map<String, String> category321 = new HashMap<String, String>();
    category123.put("price_score", "0.2");
    category123.put("confidence_score", "0.4");
    Map<String, Object> categories = new HashMap<String, Object>();
    categories.put("123", category123);
    categories.put("321", category321);
    props.put("categories", categories);  
    

    解决方案 2:
    或者您可以使用其他类来简化它,例如:

    public class Category 
    {
       private String price_score;
       private String confidence_score;
    
       public Category(String price_score, String confidence_score) 
       {
          this.price_score = price_score;
          this.confidence_score = confidence_score;
       }
    
       public Category() 
       {
       } 
       // getters/setters
    }
    

    主要方法

    Map<String, Object> props = new HashMap<String, Object>();
    props.put("site-id", 0);
    props.put("price-score", 0.5);
    props.put("confidence-score", 0.2);
    Map<String, Category> categories = new HashMap<String, Category>();
    categories.put("123", new Category("0.4", "0.2"));
    categories.put("321", new Category("0.2", "0.5"));
    props.put("categories", categories);
    

    【讨论】:

    • 它适用于您的第一个解决方案。但我更渴望采用您的第二个解决方案。一旦我使用您的第二个解决方案,我总是会收到此错误 - Throwable occurred: org.codehaus.jackson.map.JsonMappingException: No serializer found for class com.host.experiment.jackson.JacksonTest.MainClass$Category and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS) ) (through reference chain: com.host.experiment.jackson.JacksonTest.AttributeValue["v"]-&gt;java.util.HashMap["categories"]-&gt;java.util.HashMap["321"])
    • @llya:知道为什么会这样吗?
    • 好的.. 现在可以了。我忘了生成 getter 和 setter。谢谢..但是在您看来,哪种方式更受欢迎?解决方案 1 还是 2?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-02
    • 2015-10-05
    • 2017-12-10
    • 2014-02-12
    • 1970-01-01
    • 2015-04-29
    相关资源
    最近更新 更多