Tag: jackson

展平JSON字符串以使用Gson或Jackson将包含每个级别键值的键设置为Map

我有一个关于使用Gson或Jackson将JSON字符串Flatten到Map的增强问题。 我的方案包括重复的密钥,因此上述问题中的解决方案将导致一些重复的密钥被覆盖。 所以我想通过将每个级别的键组合在一起来构造键。 那么如何实现呢? 例如: { “id” : “123”, “name” : “Tom”, “class” : { “subject” : “Math”, “teacher” : “Jack” } } 我想得到地图: “id” : “123”, “name” : “Tom”, “class.subject” : “Math”, “class.teacher” : “Jack” ************************更新解决方案************************ ************* 根据@Manos Nikolaidis的回答,我可以通过考虑ArrayNode来实现以下解决方案。 public void processJsonString(String jsonString) throws Exception { ObjectMapper mapper = new ObjectMapper(); ArrayNode arrayNode = […]

使用jackson将json反序列化为java – 特殊字符问题

我使用jackson(泽西和docker)作为我的REST网络服务 – 一切进展顺利。 但是我要求在json post请求中的一个名称值对中包含一个特殊字符。 即json请求(在post正文中) – { “id”: “1”, “print-color” : “red” } //“print-color”中的“ – ”给出了问题。 现在在我对应的这个对象Item.java类的java bean中,我不能创建一个名为print-color的属性(因为不允许使用“ – ”)。 如何在映射中处理它? 谢谢。

带有动态密钥名称的Json String to Java Object

我正在尝试将这个结构化的json字符串解析为Java Object,但我没有尝试过每次尝试。 { “message”: “Test Message”, “status”: true, “users”: { “user_xy”: [ { “time”: “2016-08-25 19:01:20.944614158 +0300 EEST”, “age”: 24, “props”: { “pr1”: 197, “pr2”: 0.75, “pr3”: 0.14, “pr4”: -0.97 } } ], “user_zt”: [ { “time”: “2016-08-25 17:08:36.920891187 +0300 EEST”, “age”: 29, “props”: { “pr1”: 1.2332131860505051, “pr2”: -0.6628148829634317, “pr3”: -0.11622442112006928 } } ] } […]

jackson – 反序列化失败了循环依赖

好的,所以我试图用jacksonjson转换器测试一些东西。 我正在尝试模拟图形行为,因此这些是我的POJO实体 @JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = “id”) public class ParentEntity implements java.io.Serializable { private String id; private String description; private ParentEntity parentEntity; private List parentEntities = new ArrayList(0); private List children = new ArrayList(0); // … getters and setters } @JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = “id”) public class ChildEntity implements java.io.Serializable { private String […]

jaxrs找不到我的joda.money类型的自定义(de)序列化程序

我已经为joda.money.Money类型编写了我的自定义(de)序列化joda.money.Money 。 我用Object Mapper注册它们。 但是当我部署我的war文件时,它说找不到joda.money.Money类型的序列化器。 import org.joda.money.Money; import com.fasterxml.jackson.core.JsonParser; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.DeserializationContext; import com.fasterxml.jackson.databind.deser.std.StdDeserializer; public class MoneyDeserializer extends StdDeserializer { private static final long serialVersionUID = 1L; public MoneyDeserializer() { super(Money.class); } @Override public Money deserialize(JsonParser parser, DeserializationContext context) throws IOException, JsonProcessingException { … } } 在ObjectMapper中注册; import org.codehaus.jackson.jaxrs.JacksonJsonProvider; import org.joda.money.Money; import com.fasterxml.jackson.core.Version; import […]

单个自定义序列化程序,用于所有嵌入的带注释的对象,用它们的id替换它们

我有这样的实体: @Entity public Product { @Id public int id; public String name; @ManyToOne(cascade = {CascadeType.DETACH} ) Category category @ManyToMany(cascade = {CascadeType.DETACH} ) Set secondaryCategories; } 和 @Entity public Category { @Id public int id; public String name; @JsonCreator public Category(int id) { this.id = id; } public Category() {} } 是否可以使用注释来注释Category类或category和secondaryCategories属性,这些注释将它们序列化为嵌入时的ID。 现在,当我为id = 1的产品进行GET时,我从服务器获取: { […]

与JS.fasterxml.jackson JacksonJaxbJsonProvider的@XMLElementWrapper问题

我正在尝试获取正确的JSON public class MyTestResponse { @XmlElementWrapper(name = “data”) @XmlElement(name = “values”) public List test = Arrays.asList(“Sidney”); } 我现在明白了 “values”: [ “Sidney” ], 代替 “data”:{ “values”: [ “Sidney” ] }, 所以包装元素“data”不存在。 我在ServiceMix 7 M3中使用com.fasterxml.jackson堆栈(2.8.6)。 我的JSON提供程序扩展了com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider: import com.fasterxml.jackson.databind.AnnotationIntrospector; import com.fasterxml.jackson.databind.MapperFeature; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.module.jaxb.JaxbAnnotationIntrospector; import com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider; @Provider @Consumes({ MediaType.APPLICATION_JSON, “text/json” }) @Produces({ MediaType.APPLICATION_JSON, “text/json” }) public class […]

如何根据json中的属性编写jackson反序列化器

我想在类Type上编写json反序列化器,这样当基于名称从给定的json反序列化Type时,它会根据某些工厂方法将值(类型为接口Being)映射到其当前实现,该工厂方法根据名称返回正确的类名,并填充剩余的类没有任何明确的反序列化,也没有明确地使用new创建TigerBeing或HumanBeing的对象。 我试图使用@jsonCreator,但我必须使用new初始化整个HumanBeing或TigerBeing并在构造函数中传递所有json。 我需要自动映射进一步用作进一步pojo的类型可能非常复杂。 {type:[{ “name”: “Human”, “value”: { “height”:6, “weight”:100, “languages”:[“spanish”,”english”] } }, { “name”:”Tiger”, “value”:{ “extinct”:1, “found”:[“Asia”, “America”, “Europe”, “Africa”] } } ]} I have: public class Type { String name; Being value; } public interface Being { } public class TigerBeing implements Being { Integer extinct; String[] found; } public class HumanBeing implement […]

JsonMappingException:无法从START_OBJECT标记中反序列化java.lang.Integer的实例

我想用Spring Boot编写一个小而简单的REST服务。 这是REST服务代码: @Async @RequestMapping(value = “/getuser”, method = POST, consumes = “application/json”, produces = “application/json”) public @ResponseBody Record getRecord(@RequestBody Integer userId) { Record result = null; // Omitted logic return result; } 我发送的JSON对象如下: { “userId”: 3 } 以下是我得到的例外情况: WARN 964 — [XNIO-2 task-7] .wsmsDefaultHandlerExceptionResolver:无法读取HTTP消息:org.springframework.http.converter.HttpMessageNotReadableException:无法读取文档:无法从START_OBJECT反序列化java.lang.Integer的实例令牌在[来源:java.io.PushbackInputStream@12e7333c; line:1,column:1]; 嵌套exception是com.fasterxml.jackson.databind.JsonMappingException:无法在[来源:java.io.PushbackInputStream@12e7333c;中的START_OBJECT标记中反序列化java.lang.Integer的实例。 line:1,column:1]

jackson将数组反序列化为java对象

我有第三方编码的json’ed host, port, uri元组数组作为固定长度数组的数组: [ [“www1.example.com”, “443”, “/api/v1”], [“proxy.example.com”, “8089”, “/api/v4”] ] 我想用jackson magic获取一个实例列表 class Endpoint { String host; int port; String uri; } 请帮我设置正确的注释,使ObjectMapper能够发挥魔力。 我无法控制传入的格式,所有我的google’n都以如何将正确的json对象(不是数组)数组映射到对象列表(如https://stackoverflow.com/a/6349488/707608 )的答案结束) === https://stackoverflow.com/users/59501/staxman在https://stackoverflow.com/a/38111311/707608建议的工作解决方案 public static void main(String[] args) throws IOException { String input = “” + “[\n” + ” [\”www1.example.com\”, \”443\”, \”/api/v1\”],\n” + ” [\”proxy.example.com\”, \”8089\”, \”/api/v4\”]\n” + “]”; […]