org.codehaus.jackson和com.fasterxml.jackson.core有关吗?我有
org.codehaus.jackson jackson-all version 1.7.2
和
com.fasterxml.jackson.core> jackson-databind版本2.4.3
在我的pom.我不确定它们是否多余且可能发生冲突.
我需要在Java中更改JSON属性的值,我可以正确获取值但是我无法修改JSON.
这是下面的代码
JsonNode blablas = mapper.readTree(parser).get("blablas");
for (JsonNode jsonNode : blablas) {
String elementId = jsonNode.get("element").asText();
String value = jsonNode.get("value").asText();
if (StringUtils.equalsIgnoreCase(elementId, "blabla")) {
if(value != null && value.equals("YES")){
// I need to change the node to NO then save it into the JSON
}
}
}
Run Code Online (Sandbox Code Playgroud)
做这个的最好方式是什么?
我有String以下值:
[
{
"key1": "value11",
"key2": "value12"
},
{
"key1": "value21",
"key2": "value22"
}
]
Run Code Online (Sandbox Code Playgroud)
以下课程:
public class SomeClass {
private String key1;
private String key2;
/* ... getters and setters omitted ...*/
}
Run Code Online (Sandbox Code Playgroud)
我想将它解析为a List<SomeClass>或aSomeClass[]
使用杰克逊 这是最简单的方法ObjectMapper吗?
在Jackson中,当你用一个构造函数注释时@JsonCreator,你必须用它来注释它的参数@JsonProperty.所以这个构造函数
public Point(double x, double y) {
this.x = x;
this.y = y;
}
Run Code Online (Sandbox Code Playgroud)
成为这个:
@JsonCreator
public Point(@JsonProperty("x") double x, @JsonProperty("y") double y) {
this.x = x;
this.y = y;
}
Run Code Online (Sandbox Code Playgroud)
我不明白为什么这是必要的.你能解释一下吗?
我正在使用Spring Boot(1.2.1),其方式与构建RESTful Web服务教程的方式类似:
@RestController
public class EventController {
@RequestMapping("/events/all")
EventList events() {
return proxyService.getAllEvents();
}
}
Run Code Online (Sandbox Code Playgroud)
所以上面,Spring MVC暗中使用Jackson将我的EventList对象序列化为JSON.
但我想对JSON格式进行一些简单的自定义,例如:
setSerializationInclusion(JsonInclude.Include.NON_NULL)
Run Code Online (Sandbox Code Playgroud)
问题是,自定义隐式JSON映射器的最简单方法是什么?
我在这篇博客文章中尝试了这种方法,创建了一个CustomObjectMapper等等,但是第3步"在Spring上下文中注册类"失败了:
org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'jacksonFix': Injection of autowired dependencies failed;
nested exception is org.springframework.beans.factory.BeanCreationException:
Could not autowire method: public void com.acme.project.JacksonFix.setAnnotationMethodHandlerAdapter(org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter);
nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException:
No qualifying bean of type [org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter]
found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. …Run Code Online (Sandbox Code Playgroud) 我在下面有一个Enum desrcibed:
public enum OrderType {
UNKNOWN(0, "Undefined"),
TYPEA(1, "Type A"),
TYPEB(2, "Type B"),
TYPEC(3, "Type C");
private Integer id;
private String name;
private WorkOrderType(Integer id, String name) {
this.id = id;
this.name = name;
}
//Setters, getters....
}
Run Code Online (Sandbox Code Playgroud)
我使用我的controller(new OrderType[] {UNKNOWN,TYPEA,TYPEB,TYPEC};)返回枚举数组,然后Spring将其序列化为以下json字符串:
["UNKNOWN", "TYPEA", "TYPEB", "TYPEC"]
Run Code Online (Sandbox Code Playgroud)
迫使杰克逊像POJO一样序列化枚举的最佳方法是什么?例如:
[
{"id": 1, "name": "Undefined"},
{"id": 2, "name": "Type A"},
{"id": 3, "name": "Type B"},
{"id": 4, "name": "Type C"}
]
Run Code Online (Sandbox Code Playgroud)
我玩了不同的注释但无法获得这样的结果.
使用jackson 2.1,如何禁用fail_on_empty beans错误消息似乎要我禁用?
我认为这只是世界上最简单的事情,但是现在已经很晚了,我还没能找到一个简单的教程或任何特别明显的教程api.SerializationFactory?为什么他们会让它变得如此不直观,然后使错误信息看起来如此简单?
虽然我做喜欢的错误讯息,我的意思是,它比NPE更好.
我假设有一种方法可以使用注释来实现这一点 - 但我并不热衷于使用它们来完成我正在做的简单工作!
有没有办法使用Jackson JSON处理器进行自定义字段级序列化?例如,我想要上课
public class Person {
public String name;
public int age;
public int favoriteNumber;
}
Run Code Online (Sandbox Code Playgroud)
序列化为以下JSON:
{ "name": "Joe", "age": 25, "favoriteNumber": "123" }
Run Code Online (Sandbox Code Playgroud)
注意,age = 25被编码为数字而favoriteNumber = 123被编码为字符串.开箱即用杰克逊编组int了一个号码.在这种情况下,我希望favoriteNumber被编码为字符串.
给出以下.json文件:
[
{
"name" : "New York",
"number" : "732921",
"center" : [
"latitude" : 38.895111,
"longitude" : -77.036667
]
},
{
"name" : "San Francisco",
"number" : "298732",
"center" : [
"latitude" : 37.783333,
"longitude" : -122.416667
]
}
]
Run Code Online (Sandbox Code Playgroud)
我准备了两个类来表示包含的数据:
public class Location {
public String name;
public int number;
public GeoPoint center;
}
Run Code Online (Sandbox Code Playgroud)
...
public class GeoPoint {
public double latitude;
public double longitude;
}
Run Code Online (Sandbox Code Playgroud)
为了解析.json文件中的内容,我使用Jackson 2.2.x并准备了以下方法:
public static List<Location> getLocations(InputStream inputStream) {
ObjectMapper objectMapper …Run Code Online (Sandbox Code Playgroud) 我有一个Java类MyPojo,我有兴趣从JSON反序列化.我已经配置了一个特殊的MixIn类MyPojoDeMixIn,以帮助我进行反序列化.MyPojo只有int和String实例变量结合适当的getter和setter.MyPojoDeMixIn看起来像这样:
public abstract class MyPojoDeMixIn {
MyPojoDeMixIn(
@JsonProperty("JsonName1") int prop1,
@JsonProperty("JsonName2") int prop2,
@JsonProperty("JsonName3") String prop3) {}
}
Run Code Online (Sandbox Code Playgroud)
在我的测试客户端中,我执行以下操作,但当然它在编译时不起作用,因为存在JsonMappingException与类型不匹配相关的问题.
ObjectMapper m = new ObjectMapper();
m.getDeserializationConfig().addMixInAnnotations(MyPojo.class,MyPojoDeMixIn.class);
try { ArrayList<MyPojo> arrayOfPojo = m.readValue(response, MyPojo.class); }
catch (Exception e) { System.out.println(e) }
Run Code Online (Sandbox Code Playgroud)
我知道我可以通过创建一个只包含其中的"Response"对象来缓解这个问题ArrayList<MyPojo>,但是我必须为我想要返回的每个类型创建这些有些无用的对象.
我还在线查看了JacksonInFiveMinutes,但是很难理解有关Map<A,B>我的问题以及它与我的问题有什么关系.如果你不能说,我是Java的新手,来自Obj-C背景.他们特别提到:
除了绑定到POJO和"简单"类型之外,还有一个附加变体:绑定到通用(类型)容器的变体.这种情况需要特殊处理,因为所谓的类型擦除(Java用于以某种向后兼容的方式实现泛型),这会阻止您使用类似Collection.class(不能编译)的东西.
因此,如果要将数据绑定到Map,则需要使用:
Map<String,User> result = mapper.readValue(src, new TypeReference<Map<String,User>>() { });
Run Code Online (Sandbox Code Playgroud)
我怎样才能直接反序列化ArrayList?