用Jackson将Java对象转换成JSON

我想我的JSON看起来像这样:

{ "information": [{ "timestamp": "xxxx", "feature": "xxxx", "ean": 1234, "data": "xxxx" }, { "timestamp": "yyy", "feature": "yyy", "ean": 12345, "data": "yyy" }] } 

目前代码:

 import java.util.List; public class ValueData { private List<ValueItems> information; public ValueData(){ } public List<ValueItems> getInformation() { return information; } public void setInformation(List<ValueItems> information) { this.information = information; } @Override public String toString() { return String.format("{information:%s}", information); } } 

 public class ValueItems { private String timestamp; private String feature; private int ean; private String data; public ValueItems(){ } public ValueItems(String timestamp, String feature, int ean, String data){ this.timestamp = timestamp; this.feature = feature; this.ean = ean; this.data = data; } public String getTimestamp() { return timestamp; } public void setTimestamp(String timestamp) { this.timestamp = timestamp; } public String getFeature() { return feature; } public void setFeature(String feature) { this.feature = feature; } public int getEan() { return ean; } public void setEan(int ean) { this.ean = ean; } public String getData() { return data; } public void setData(String data) { this.data = data; } @Override public String toString() { return String.format("{timestamp:%s,feature:%s,ean:%s,data:%s}", timestamp, feature, ean, data); } } 

我只是错过了我如何可以将Java对象转换为与jacksonJSON的部分:

 public static void main(String[] args) { // CONVERT THE JAVA OBJECT TO JSON HERE System.out.println(json); } 

我的问题是:我的课是否正确? 我必须调用哪个实例以及如何实现此JSON输出?

用JSON把你的object转换成JSON:

 ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter(); String json = ow.writeValueAsString(object); 

这可能是有用的………

 objectMapper.writeValue(new File("c:\\employee.json"), employee); // display to console Object json = objectMapper.readValue( objectMapper.writeValueAsString(employee), Object.class); System.out.println(objectMapper.writerWithDefaultPrettyPrinter() .writeValueAsString(json)); 

我知道这是旧的(我是新来的Java),但我遇到了同样的问题。 答案对我来说不像新手那么清楚…所以我想我会补充我学到的东西。

我使用了第三方库来帮助您: org.codehaus.jackson所有的下载都可以在这里find。

对于基本的JSONfunction,您需要将以下jar添加到项目的库中: jackson-mapper-asl和jackson-core-asl

select你的项目需要的版本。 (通常你可以使用最新的稳定版本)。

将它们导入到项目库后,将以下import行添加到代码中:

  import org.codehaus.jackson.JsonGenerationException; import org.codehaus.jackson.map.JsonMappingException; import org.codehaus.jackson.map.ObjectMapper;` 

通过java对象定义和分配值,您希望将其转换为JSON并作为REST式Web服务的一部分返回

 User u = new User(); u.firstName = "Sample"; u.lastName = "User"; u.email = "sampleU@example.com"; ObjectMapper mapper = new ObjectMapper(); try { // convert user object to json string and return it return mapper.writeValueAsString(u); } // catch various errors catch (JsonGenerationException e) { e.printStackTrace(); } catch (JsonMappingException e) { e.printStackTrace(); } 

结果应该如下所示: {"firstName":"Sample","lastName":"User","email":"sampleU@example.com"}

就这样做

 Gson gson = new Gson(); return Response.ok(gson.toJson(yourClass)).build(); 

那么,即使被接受的答案也不完全是输出所要求的。 它输出的是JSONstring,但是"字符被转义了,所以虽然可能有点晚了,但我正在回答,希望它能帮助人们!我是这样做的:

 StringWriter writer = new StringWriter(); JsonGenerator jgen = new JsonFactory().createGenerator(writer); jgen.setCodec(new ObjectMapper()); jgen.writeObject(object); jgen.close(); System.out.println(writer.toString()); 
 public class JSONConvector { public static String toJSON(Object object) throws JSONException, IllegalAccessException { String str = ""; Class c = object.getClass(); JSONObject jsonObject = new JSONObject(); for (Field field : c.getDeclaredFields()) { field.setAccessible(true); String name = field.getName(); String value = String.valueOf(field.get(object)); jsonObject.put(name, value); } System.out.println(jsonObject.toString()); return jsonObject.toString(); } public static String toJSON(List list ) throws JSONException, IllegalAccessException { JSONArray jsonArray = new JSONArray(); for (Object i : list) { String jstr = toJSON(i); JSONObject jsonObject = new JSONObject(jstr); jsonArray.put(jsonArray); } return jsonArray.toString(); } }