如何在 Java 中对投影属性进行字符串化?
How to stringfy projection properties in Java?
我通过连接两个表来填充以下投影,它 return 只有一条记录:
public interface EmployeeProjection {
UUID getEmployeeUuid();
String getEmployeeName();
UUID getCompanyUuid();
String getCompanyName();
}
我想 return 将 EmployeeProjection
中的这条记录输入到我的二维码中,因此我想将此数据转换为数组或 JSON。那么,我该如何管理呢?
你可以使用我推荐的 jackson API 来实现它。
EmployeeProjection obj = new EmployeeProjection();
StringWriter jsonString = new StringWriter();
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.writeValue(jsonString, obj);
System.out.println("Employee JSON is: " + jsonString);
这是输出。
{"EmployeeUuid":"1","EmployeeName":"John" ... }
这是图书馆。
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.13.0</version>
</dependency>
进一步处理:
JsonFactory jsonFactory = new JsonFactory();
JsonParser jp = jsonFactory.createJsonParser(jsonString.toString());
jp.setCodec(new ObjectMapper());
JsonNode jsonNode = jp.readValueAsTree();
// loop through json node
Iterator<Map.Entry<String, JsonNode>> fields = jsonNode.fields();
while (fields.hasNext()) {
Map.Entry<String, JsonNode> entry = fields.next();
// Output key and text value
System.out.println(entry.getKey() + " " + entry.getValue().textValue());
}
我通过连接两个表来填充以下投影,它 return 只有一条记录:
public interface EmployeeProjection {
UUID getEmployeeUuid();
String getEmployeeName();
UUID getCompanyUuid();
String getCompanyName();
}
我想 return 将 EmployeeProjection
中的这条记录输入到我的二维码中,因此我想将此数据转换为数组或 JSON。那么,我该如何管理呢?
你可以使用我推荐的 jackson API 来实现它。
EmployeeProjection obj = new EmployeeProjection();
StringWriter jsonString = new StringWriter();
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.writeValue(jsonString, obj);
System.out.println("Employee JSON is: " + jsonString);
这是输出。
{"EmployeeUuid":"1","EmployeeName":"John" ... }
这是图书馆。
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.13.0</version>
</dependency>
进一步处理:
JsonFactory jsonFactory = new JsonFactory();
JsonParser jp = jsonFactory.createJsonParser(jsonString.toString());
jp.setCodec(new ObjectMapper());
JsonNode jsonNode = jp.readValueAsTree();
// loop through json node
Iterator<Map.Entry<String, JsonNode>> fields = jsonNode.fields();
while (fields.hasNext()) {
Map.Entry<String, JsonNode> entry = fields.next();
// Output key and text value
System.out.println(entry.getKey() + " " + entry.getValue().textValue());
}