使用Jackson将Java对象转换为JSON
我希望我的JSON如下所示:使用Jackson将Java对象转换为JSON,java,json,object,jackson,Java,Json,Object,Jackson,我希望我的JSON如下所示: { "information": [{ "timestamp": "xxxx", "feature": "xxxx", "ean": 1234, "data": "xxxx" }, { "timestamp": "yyy", "feature": "yyy", "ean": 12345, "data": "yyy"
{
"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);
}
}
我只是错过了如何使用Jackson将Java对象转换为JSON的部分:
public static void main(String[] args) {
// CONVERT THE JAVA OBJECT TO JSON HERE
System.out.println(json);
}
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter();
String json = ow.writeValueAsString(object);
我的问题是:我的课程正确吗?我必须调用哪个实例以及如何实现此JSON输出?要使用Jackson转换JSON中的
对象
:
public static void main(String[] args) {
// CONVERT THE JAVA OBJECT TO JSON HERE
System.out.println(json);
}
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
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));
只需遵循以下任何一项:
- 对于jackson它应该可以工作:
ObjectMapper mapper = new ObjectMapper(); return mapper.writeValueAsString(object); //will return json in string
Gson gson = new Gson(); return Response.ok(gson.toJson(yourClass)).build();
- 对于gson,它应该可以工作:
ObjectMapper mapper = new ObjectMapper(); return mapper.writeValueAsString(object); //will return json in string
Gson gson = new Gson(); return Response.ok(gson.toJson(yourClass)).build();
org.codehaus.jackson
所有的下载都可以找到
对于基本JSON功能,您需要将以下JAR添加到项目的库中:
和
选择项目所需的版本。(通常可以使用最新的稳定版本)
将它们导入到项目库后,将以下import
行添加到代码中:
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
使用java对象定义和分配的值,您希望将这些值转换为JSON并作为RESTful 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 (JsonGenerationException | JsonMappingException e) {
// catch various errors
e.printStackTrace();
}
结果应该如下所示:
{“firstName”:“Sample”,“lastName”:“User”,“email”:sampleU@example.com“}
好吧,即使是公认的答案也不能准确地输出op所要求的内容。它输出JSON字符串,但转义了“
个字符。因此,虽然可能有点晚了,但我希望它能帮助人们!我是这样做的:
StringWriter writer = new StringWriter();
JsonGenerator jgen = new JsonFactory().createGenerator(writer);
jgen.setCodec(new ObjectMapper());
jgen.writeObject(object);
jgen.close();
System.out.println(writer.toString());
注意:要使投票最多的解决方案发挥作用,POJO中的属性必须是公共的,或者具有公共的getter/
setter
:
默认情况下,Jackson 2将只处理以下字段:
public,或具有公共getter方法–序列化
将所有字段设置为私有或包私有将失败
尚未测试,但我相信此规则也适用于其他JSON库,如google Gson。您可以这样做:
String json = new ObjectMapper().writeValueAsString(yourObjectHere);
你可以像这样使用谷歌Gson
UserEntity user = new UserEntity();
user.setUserName("UserName");
user.setUserAge(18);
Gson gson = new Gson();
String jsonStr = gson.toJson(user);
下面的教程可能会有所帮助:唯一的事情是字符串从ObjectWriter转义出来如果它是通过RESTful等Web服务发送的。什么是Response.ok?当我打开我的Gson对象时,我发现插入mongoDB的JSON大小超出了允许的限制。只是一个提示。问题是关于
Jackson
,而不是Gson
@Codeversed Response是Jers中的一个类ey library.Response.ok将返回状态代码为200的JSON响应。太多的工作,太多的反射!映射程序应该让你不用做这些样板文件!我喜欢Converctor!