Tag: jackson2

使用Jackson将Java对象实例编写到YAML

我有一个’示例’Pojo类,如下所述。 任何一个tel都可以使用Jackson将Example类的实例保存到YAML文件中。 public class Example { String name; int value; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getValue() { return value; } public void setValue(int value) { this.value = value; } }

在Jackson中将JsonNode序列化为非常特定的JSON格式

我有JsonNode结果,我想要打印出来。 到目前为止,我正在使用: ObjectMapper mapper = new ObjectMapper(); mapper.enable(SerializationFeature.INDENT_OUTPUT); File outputFile = new File( getCurOutputDir(), String.format(“out.json”, getClass().getSimpleName()) ); mapper.writeValue(new FileOutputStream(outputFile), resultNode); 输出如下: { “A” : [ { “Ai” : { “Ai1” : 42, “Ai2” : 55 } } ], “B” : [ 86 ] } 但我需要它采用这种特定的格式: { “A” : [ { “Ai” : { “Ai1” : […]

将嵌套的json转换为点符号json

我有一个服务,我得到一个json字符串响应,如下所示 { “id”: “123”, “name”: “John” } 我使用HttpClient消耗rest调用并将json字符串转换为Map ,如下所示。 String url= “http://www.mocky.io/v2/5979c2f5110000f4029edc93”; HttpClient client = HttpClientBuilder.create().build(); HttpGet httpGet = new HttpGet(url); httpGet.setHeader(“Content-Type”, “application/json”); HttpResponse httpresponse = client.execute(httpGet); String response = EntityUtils.toString(httpresponse.getEntity()); ObjectMapper mapper = new ObjectMapper(); Map map = mapper.readValue(response, new TypeReference<Map>(){}); 从json字符串到HashMap的转换工作正常,但实际上我的要求是有时在主json中可能有一些嵌套的json,例如在下面的json我有一个额外的address键,这又是一个嵌套的json有city和town细节。 { “id”: “123”, “name”: “John”, “address”: { “city”: “Chennai”, “town”: “Guindy” } […]

如何使用jackson制作POJO并解析递归对象?

我有一个以下JSON响应,我从rest服务回来。 现在我需要将JSON响应下面的反序列化反转为POJO。 我正在和jackson合作。 { “pagination”: { “number”: 1, “entriesPerPage”: 200, “total”: 3 }, “postings”: [{ “categories”: [{ “taskid”: “79720”, “name”: “Sunglasses”, “parentCategory”: { “taskid”: “394”, “name”: “Sunglasses & Fashion Eyewear”, “parentCategory”: { “taskid”: “2340”, “name”: “Men’s Accessories”, “parentCategory”: { “taskid”: “10987”, “name”: “Clothing, Shoes & Accessories” } } } }] }, { “categories”: [{ “taskid”: […]

如何在循环引用中使用@JsonIdentityInfo?

我正在尝试使用Jackson 2中的@JsonIdentityInfo,如此处所述。 出于测试目的,我创建了以下两个类: public class A { private B b; // constructor(s) and getter/setter omitted } public class B { private A a; // see above } 当然,天真的方法很糟糕: @Test public void testJacksonJr() throws Exception { A a = new A(); B b = new B(a); a.setB(b); String s = JSON.std.asString(a);// throws StackOverflowError Assert.assertEquals(“{\”@id\”:1,\”b\”:{\”@id\”:2,\”a\”:1}}”, s); } […]