猿问

为集合中的每个元素调用JsonGenerator方法

我有一种方法,可以使用JsonGenerator将产品对象写入文件。(productToJSON)

我想使用第二种方法,使用JsonGenerator流将HashSet中的所有产品写入JSON文件。我相信打开和关闭流(文件仅包含最后一个产品)存在问题。因此,我可以使用第二种方法打开和关闭流,但是第一种方法不再适用于一种产品。

请告诉我:

  1. 是否可以使用2种方法做我想做的事?

  2. 有更好的方法吗?

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);

    }

}


青春有我
浏览 203回答 2
2回答

慕姐8265434

方法一-始终调用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();}

哈士奇WWW

您可以使用ObjectMapper从Java Object创建一个json。[以及POJO中的Setter-Getter方法]。使用DefaultPrettyPrinter创建ObjectWriter。注意:无需在JsonGenerator中设置属性。喜欢,jsonGen.writeStringField("name", product.getName());请参考以下示例。&nbsp; &nbsp; import java.io.File;&nbsp; &nbsp; import java.util.ArrayList;&nbsp; &nbsp; import java.util.List;&nbsp; &nbsp; import com.fasterxml.jackson.core.util.DefaultPrettyPrinter;&nbsp; &nbsp; import com.fasterxml.jackson.databind.ObjectMapper;&nbsp; &nbsp; import com.fasterxml.jackson.databind.ObjectWriter;&nbsp; &nbsp; public class Demo {&nbsp; &nbsp; &nbsp; &nbsp; public static void main(String[] args) throws Exception {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; List<Product> list = new ArrayList<Product>();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; list.add(new Product("Sample Product", 123012));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; list.add(new Product("Sample 1 Product 1", 2134));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; list.add(new Product("Sample 1 Product 2", 4353));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; list.add(new Product("Sample 1 Product 3", 34345));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; productsToJSON(list, "Test.txt");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; private static void productsToJSON(List<Product> products, String fileName) throws Exception {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ObjectMapper mapper = new ObjectMapper();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ObjectWriter writer = mapper.writer(new DefaultPrettyPrinter());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; writer.writeValue(new File(fileName), products);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; class Product {&nbsp; &nbsp; &nbsp; &nbsp; private String name;&nbsp; &nbsp; &nbsp; &nbsp; private Integer weight;&nbsp; &nbsp; &nbsp; &nbsp; public Product(String name, Integer weight) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.name = name;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.weight = weight;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; public String getName() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return name;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; public void setName(String name) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.name = name;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; public Integer getWeight() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return weight;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; public void setWeight(Integer weight) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.weight = weight;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }输出:&nbsp; &nbsp; [ {&nbsp; &nbsp; &nbsp; "name" : "Sample Product",&nbsp; &nbsp; &nbsp; "weight" : 123012&nbsp; &nbsp; }, {&nbsp; &nbsp; &nbsp; "name" : "Sample1 Product1",&nbsp; &nbsp; &nbsp; "weight" : 123012&nbsp; &nbsp; } ]
随时随地看视频慕课网APP

相关分类

Java
我要回答