Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将数据写入json文件_Java_Arrays_Json - Fatal编程技术网

Java 将数据写入json文件

Java 将数据写入json文件,java,arrays,json,Java,Arrays,Json,我需要在JSON文件中写入一些信息 我编写了以下函数: public String toJSON() { StringBuffer sb = new StringBuffer(); sb.append("\"" + MyConstants.VEHICLE_LABEL + "\":"); sb.append("{"); sb.append("\"" + MyConstants.CAPACITY1_LABEL + "\": " + String.valueOf(th

我需要在JSON文件中写入一些信息

我编写了以下函数:

public String toJSON() {
    StringBuffer sb = new StringBuffer();

    sb.append("\"" + MyConstants.VEHICLE_LABEL + "\":");
    sb.append("{");
    sb.append("\"" + MyConstants.CAPACITY1_LABEL + "\": " + String.valueOf(this.getCapacity(0)) +  ",");
    sb.append("\"" + MyConstants.CAPACITY2_LABEL + "\": " + String.valueOf(this.getCapacity(1)) +  ",");
    sb.append("\"" + MyConstants.CAPACITY3_LABEL + "\": " + String.valueOf(this.getCapacity(2)));   
    sb.append("}");

    return sb.toString();
}
但是,我想让这个函数更灵活。特别是,如果容量单位的数量不是固定的(1、2或3),我应该如何更改此代码


我认为应该有一个FOOR循环,但是我不确定如何正确地实现它。

只有当
this.getCapacity(I)
不是空的时候,才能执行追加

您可以使用for循环执行类似的操作

for(int i=0; i < max; i++) {
    if(!StringUtils.isEmpty(String.valueOf(this.getCapacity(i)))){
        sb.append("\"" + String.format(MyConstants.CAPACITY_LABEL, i) + "\": " + String.valueOf(this.getCapacity(i)) +  ",");
    }
}
for(int i=0;i
其中
MyConstants.CAPACITY\u LABEL
类似于“CAPACITY%d\u LABEL”


但是,正如azurefrog所说,我将使用json解析器来实现这一点。

您可以尝试以下遵循流行构建器模式的类:

public class JsonVehicleBuilder {

    private StringBuilder builder;

    /**
     *  Starts off the builder with the main label
     */
    public JsonVehicleBuilder(String mainLabel) {
        builder = new StringBuilder();
        builder.append("\"").append(mainLabel).append("\":");
        builder.append("{");
    }

    public JsonVehicleBuilder appendSimpleValue(String label, String value) {
        builder.append("\"").append(label).append("\":").append(value).append(",");
        return this;
    }

    /**
     * Appends the closing bracket and outputs the final JSON
     */
    public String build() {
        builder.deleteCharAt(builder.lastIndexOf(",")); //remove last comma
        builder.append("}");
        return builder.toString();
    }
}
然后在main方法中调用:

   JsonVehicleBuilder jsonVehicleBuilder = new JsonVehicleBuilder(MyConstants.VEHICLE_LABEL);
    jsonVehicleBuilder.appendSimpleValue(MyConstants.CAPACITY1_LABEL,String.valueOf(this.getCapacity(0)))
            .appendSimpleValue(MyConstants.CAPACITY2_LABEL,String.valueOf(this.getCapacity(1)))
            .appendSimpleValue(MyConstants.CAPACITY3_LABEL,String.valueOf(this.getCapacity(2)));

    String json = jsonVehicleBuilder.build();

然后,您可以继续链接appendSimpleValue方法,只要您愿意。

有什么原因不能使用gson或jackson之类的json解析器吗?@azurefrog:这个主题对我来说是新的。我只是在改进我从另一个人那里收到的代码。因此,如果有人能告诉我如何在不使用任何解析器的情况下基于此代码回答问题,我将不胜感激。如果有人要求我改进代码,我会建议使用适当的工具/库,而不是将一些东西拼凑在一起。如果他们不同意我的意见(或者其他人可能会被冒犯或者其他什么),并且认为重新发明轮子更好,那么我会按照他们的方式去做。是的,真正困难的部分是如果容量标签都是随机文本。