2 回答
TA贡献1813条经验 获得超2个赞
方法一-始终调用productsToJSON:
private void productToJSON(Product product, JsonGenerator jsonGen) throws Exception {
jsonGen.writeStartObject();
jsonGen.writeStringField("name", product.getName());
jsonGen.writeNumberField("weight", product.getWeight());
jsonGen.writeNumberField("stock", product.getStock());
jsonGen.writeNumberField("price", product.getPrice());
jsonGen.writeNumberField("rating", product.getRating());
jsonGen.writeEndObject();
}
public void productsToJSON(String fileName) throws Exception {
JsonFactory jsonFactory = new JsonFactory();
FileOutputStream file = new FileOutputStream(fileName);
JsonGenerator jsonGen = jsonFactory.createJsonGenerator(file, JsonEncoding.UTF8);
jsonGen.setPrettyPrinter(new DefaultPrettyPrinter());
if (this.products.size > 1) {
jsonGen.writeStartArray(this.products.size)
}
for (Product p: this.products) {
this.productToJSON(p, jsonGen);
}
if (this.products.size > 1) {
jsonGen.writeEndArray()
}
jsonGen.close();
}
方法二-一种私有方法和两种公共方法:
private void productToJSON(Product product, JsonGenerator jsonGen) throws Exception {
jsonGen.writeStartObject();
jsonGen.writeStringField("name", product.getName());
jsonGen.writeNumberField("weight", product.getWeight());
jsonGen.writeNumberField("stock", product.getStock());
jsonGen.writeNumberField("price", product.getPrice());
jsonGen.writeNumberField("rating", product.getRating());
jsonGen.writeEndObject();
}
public void productsToJSON(String fileName) throws Exception {
JsonFactory jsonFactory = new JsonFactory();
FileOutputStream file = new FileOutputStream(fileName);
JsonGenerator jsonGen = jsonFactory.createJsonGenerator(file, JsonEncoding.UTF8);
jsonGen.setPrettyPrinter(new DefaultPrettyPrinter());
jsonGen.writeStartArray(this.products.size)
for (Product p: this.products) {
this.productToJSON(p, jsonGen);
}
jsonGen.writeEndArray()
jsonGen.close();
}
public void productToJSON(Product p, String fileName) throws Exception {
JsonFactory jsonFactory = new JsonFactory();
FileOutputStream file = new FileOutputStream(fileName);
JsonGenerator jsonGen = jsonFactory.createJsonGenerator(file, JsonEncoding.UTF8);
jsonGen.setPrettyPrinter(new DefaultPrettyPrinter());
this.productToJSON(p, jsonGen);
jsonGen.close();
}
TA贡献1799条经验 获得超6个赞
您可以使用ObjectMapper从Java Object创建一个json。[以及POJO中的Setter-Getter方法]。使用DefaultPrettyPrinter创建ObjectWriter。
注意:无需在JsonGenerator中设置属性。喜欢,
jsonGen.writeStringField("name", product.getName());
请参考以下示例。
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import com.fasterxml.jackson.core.util.DefaultPrettyPrinter;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
public class Demo {
public static void main(String[] args) throws Exception {
List<Product> list = new ArrayList<Product>();
list.add(new Product("Sample Product", 123012));
list.add(new Product("Sample 1 Product 1", 2134));
list.add(new Product("Sample 1 Product 2", 4353));
list.add(new Product("Sample 1 Product 3", 34345));
productsToJSON(list, "Test.txt");
}
private static void productsToJSON(List<Product> products, String fileName) throws Exception {
ObjectMapper mapper = new ObjectMapper();
ObjectWriter writer = mapper.writer(new DefaultPrettyPrinter());
writer.writeValue(new File(fileName), products);
}
}
class Product {
private String name;
private Integer weight;
public Product(String name, Integer weight) {
this.name = name;
this.weight = weight;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getWeight() {
return weight;
}
public void setWeight(Integer weight) {
this.weight = weight;
}
}
输出:
[ {
"name" : "Sample Product",
"weight" : 123012
}, {
"name" : "Sample1 Product1",
"weight" : 123012
} ]
添加回答
举报