Java - jackson 到一个文件

标签 java json file jackson

我有一个这样的 JSON 文件:

{
    "Product":
    {
        "ID": "08-17-96-71-D9-68",

        "Licences": 
    {
        "total": 40,
        "used": 0,
        "remain": 40
        }
    }
}

我使用 jackson 将其转换为 Java 对象,并获得了所有值(到目前为止,一切顺利)。 我的问题是我想更改这些值并重新编写 JSON 文件,但是当我这样做时,结果是这样的:

"{\"Product\":{\"IaD\": \"08-17-96-71-D9-68\",\"Licences\":{\"total\": 40,\"used\": 1,\"remain\": 39}}}"

因此,当我再次尝试读取它时,它给了我一个错误,因为它无法读取第一个和最后一个字符 ("),而且它还读取了 \ 字符。

这是我的代码:

public class UsingJason {
String theJsonString = "";
ObjectMapper mapper = new ObjectMapper();

public class Product{
    Licences lic;

    public class Licences{
        int total;
        int used;
        int remain;
    }
}


public void readJson(){
    if(new File("asset/testJson.json").exists()){
    theJsonString = "";     
    try {
        BufferedReader in = new BufferedReader(new FileReader("asset/testJson.json"));
        String line;
        while ((line = in.readLine()) != null){
            theJsonString += line;
        }
        in.close();
    } catch (IOException e1) {
        e1.printStackTrace();
    }

    System.out.println("JSON String: "+ theJsonString);
}else{
    System.out.println("NO FILE FOUND");
}
JsonNode rootNode = null;
try {
    rootNode = mapper.readValue(theJsonString, JsonNode.class);
} catch (JsonParseException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
} catch (JsonMappingException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
} catch (IOException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
}
JsonNode totalNode = rootNode.get("Product").get("Licences").get("total");
JsonNode usedNode = rootNode.get("Product").get("Licences").get("used");
JsonNode remainNode = rootNode.get("Product").get("Licences").get("remain");

JsonNode idStringNode = rootNode.get("Product").get("ID");
// Parse it into a Java object.
try {
    int totalObject = mapper.readValue(totalNode, Integer.class);
    System.out.println("INTEGER? HAS TO BE... 40: "+totalObject);
    String idString = mapper.readValue(idStringNode, String.class);
    System.out.println("String? Has to be 08-17-96-71-D9-68: "+idString + "  True? " 
        + idString.equals("08-17-96-71-D9-68")  );
    int usedObject = mapper.readValue(usedNode, int.class);
    int remainObject = mapper.readValue(remainNode, int.class);


    System.out.println("Going to rest 1");

    usedObject ++;
    remainObject = totalObject - usedObject;

    String toJackson = "{\"Product\":{\"I\\D\": \"08-17-96-71-D9-68\",\"Licences\":{\"total\": "+totalObject+",\"used\": "+usedObject+",\"remain\": "+remainObject+"}}}";



    System.out.println("String created: " +toJackson);
             // THIS  toJackson String returns the string without \ and without the " 
             // IT PRINT THIS: {"Product":{"ID": "08-17-96-71-D9-68","Licences":{"total": 40,"used": 1,"remain": 39}}}
             // EXACTLY WHAT I WANT TO Write in the Json file but it writes the \ ..

mapper.writeValue(new File("asset/testJson.json"), toJackson);
} catch (JsonParseException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (JsonMappingException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

}

谁能告诉我我做错了什么?

最佳答案

在您的代码中:

mapper.writeValue(new File("asset/testJson.json"), toJackson);

您序列化的不是对象,而是文件中的字符串。我想这就是它像任何字符串一样被转义的原因。

输入值应该是具有您的结构的对象。

像这样:

// Initialize an object
Product myProduct = new Product();
myProduct.lic = new Procuct.Licences();
myProduct.lic.total = totalObject;
myProduct.lic.used = usedObject;
myProduct.lic.remain = remainObject;


// Serialize the object into JSON
mapper.writeValue(new File("asset/testJson.json"), myProduct);

关于Java - jackson 到一个文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19564292/

相关文章:

java - 通过JAVA中的方法获取数组和并返回值

arrays - 在 flutter/Dart 中解析 JSON

json - 使用接口(interface)与 Golang 映射和结构以及 JSON

json - 安装 json gem 时出错 'mkmf.rb can' t find header files for ruby​​'

python - 如何从 Microsoft Powerpoint 打开嵌入的 '.py' 文件?

java - 在 Java 中确定 Scala 对象的类

Java 将文件传递给方法错误?

java - 什么是NullPointerException,我该如何解决?

c++ - 如何在标准 C/C++ :/or\? 中获取文件分隔符

java - 创建相关 java.io.File 的安全方法