Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.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-Jackson文件_Java_Json_File_Jackson - Fatal编程技术网

Java-Jackson文件

Java-Jackson文件,java,json,file,jackson,Java,Json,File,Jackson,我有一个JSON文件,如下所示: { "Product": { "ID": "08-17-96-71-D9-68", "Licences": { "total": 40, "used": 0, "remain": 40 } } } "{\"Product\":{\"IaD\": \"08-17-96-71-D9-68\",\"Licences\":{\"total

我有一个JSON文件,如下所示:

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

        "Licences": 
    {
        "total": 40,
        "used": 0,
        "remain": 40
        }
    }
}
"{\"Product\":{\"IaD\": \"08-17-96-71-D9-68\",\"Licences\":{\"total\": 40,\"used\": 1,\"remain\": 39}}}"
// 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);
我使用jackson将其转换为Java对象,得到了所有的值(到目前为止,还不错)。 我的问题是我想更改这些值并重新写入JSON文件,但当我这样做时,结果如下:

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

        "Licences": 
    {
        "total": 40,
        "used": 0,
        "remain": 40
        }
    }
}
"{\"Product\":{\"IaD\": \"08-17-96-71-D9-68\",\"Licences\":{\"total\": 40,\"used\": 1,\"remain\": 39}}}"
// 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);
因此,当我再次尝试读取它时,它会给我一个错误,因为它无法读取第一个和最后一个字符(“),而且它还读取
\
字符

这是我的代码:

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);
序列化的不是对象,而是文件中的字符串。我想这就是它像任何字符串一样被转义的原因

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

大概是这样的:

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

        "Licences": 
    {
        "total": 40,
        "used": 0,
        "remain": 40
        }
    }
}
"{\"Product\":{\"IaD\": \"08-17-96-71-D9-68\",\"Licences\":{\"total\": 40,\"used\": 1,\"remain\": 39}}}"
// 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);
在您的代码中:

mapper.writeValue(new File("asset/testJson.json"), toJackson);
序列化的不是对象,而是文件中的字符串。我想这就是它像任何字符串一样被转义的原因

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

大概是这样的:

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

        "Licences": 
    {
        "total": 40,
        "used": 0,
        "remain": 40
        }
    }
}
"{\"Product\":{\"IaD\": \"08-17-96-71-D9-68\",\"Licences\":{\"total\": 40,\"used\": 1,\"remain\": 39}}}"
// 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);

这不是将对象映射到JSON的方式。请使用
ObjectMapper
。您可以更具体一点吗?mapper是一个ObjectMapper。这不是将对象映射到JSON的方式。请使用
ObjectMapper
。您可以更具体一点吗?mapper是一个ObjectMapper。我这样做了,我得到了一个错误,我不得不将其更改为:
Product myProduct=new Product();myProduct.lic=myProduct.new licenses();myProduct.lic.total=totalObject;myProduct.lic.used=usedObject;myProduct.lic.remaine=remainObject;myProduct.ID=idString;//将对象序列化为JSON映射器.writeValue(新文件(“asset/testJson.JSON”),myProduct)
但我遇到了一个异常:(下一个注释)异常:org.codehaus.jackson.map.JsonMappingException:没有为使用Jason$Product的类找到序列化程序,也没有发现创建BeanSerializer的属性(为了避免异常,禁用SerializationConfig.Feature.FAIL\u ON\u EMPTY\u bean))如果我在mapper.writeValue
mapper.configure(SerializationConfig.Feature.FAIL_ON_EMPTY_bean,false)之前写入;
我得到的json文件是空的:{}好的,问题是可见性。序列化的所有内容都必须公开。非常感谢DRCB!我这样做了,但我得到了一个错误,我不得不将其更改为:
Product myProduct=new Product();myProduct.lic=myProduct.new licenses();myProduct.lic.total=totalObject;myProduct.lic.used=usedObject;myProduct.lic.remaine=remainObject;myProduct.ID=idString;//将对象序列化为JSON mapper.writeValue(新文件(“asset/testJson.JSON”),myProduct);
但我得到一个异常:(下一条注释)异常:org.codehaus.jackson.map.JsonMappingException:如果我在mapper.writeValue
mapper.configure之前写入,则找不到使用Jason$Product的类的序列化程序,也找不到创建BeanSerializer的属性(为了避免异常,请禁用SerializationConfig.Feature.FAIL_ON_EMPTY_bean))(SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS,false);
我得到的json文件是空的:{}好的,问题是可见性。序列化必须公开所有内容。非常感谢您!