我有一种方法,可以使用JsonGenerator将产品对象写入文件。 (productToJSON)
我想使用第二种方法,使用JsonGenerator流将HashSet中的所有产品写入JSON文件。我认为打开和关闭流(文件仅包含最后一个产品)存在问题。因此,我可以使用第二种方法打开和关闭流,但是第一种方法不再适用于一种产品。
请让我知道:
public void productToJSON(Product product, 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.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();
jsonGen.close();
}
public void productsToJSON(String fileName) throws Exception {
for (Product p : this.products) {
this.productToJSON(p, fileName);
}
}
答案 0 :(得分:1)
一种方法-始终将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();
}
答案 1 :(得分:0)
您可以使用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
} ]