2013-10-24 79 views
0

我有這樣一個JSON文件:爪哇 - 傑克遜文件

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

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

我用傑克遜將其轉換爲一個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(); 
} 

} 

誰能告訴我什麼,我做錯了

+0

那不是你怎麼一個對象映射到JSON。使用一個「ObjectMapper」。 – 2013-10-24 11:20:19

+0

請問您能更具體地說Lutz嗎? mapper是一個ObjectMapper。 – Alberto

回答

2

在你的代碼在這裏:

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

我做到了這一點,我得到一個錯誤,我不得不將其更改爲: 'Product myProduct = new Product(); \t \t myProduct.lic = myProduct.new Licenses(); \t \t myProduct.lic.total = totalObject; \t \t myProduct.lic.used = usedObject; \t \t myProduct.lic.remain = remainingObject; \t \t myProduct.ID = idString; \t \t //將對象序列化爲JSON mapper.writeValue(new File(「asset/testJson.json」),myProduct); ' ,但我得到一個異常:(下一評論) – Alberto

+0

例外: org.codehaus.jackson.map.JsonMappingException:沒有串行發現類UsingJason $產品並沒有發現創建BeanSerializer性(避免異常,禁用SerializationConfig .Feature.FAIL_ON_EMPTY_BEANS)) 如果我的mapper.writeValue \t \t'mapper.configure(SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS,FALSE)之前寫;' JSON文件我得到的是空的:{} – Alberto

+0

確定,問題是能見度。一切都必須公開才能進行序列化。 非常感謝你! – Alberto