2017-10-14 9 views
0

jsonファイルの値を更新してファイル自体の値を置き換えるにはどうすればよいですか?私はそれを次の方法で試しました、それは値を置き換えますが、私はjsonファイルで更新された値を見ることができません。前もって感謝します。更新された値をJavaのネストされたjsonファイルに置き換えて書き込む

final FileInputStream in = new FileInputStream(System.getProperty("user.dir") + "/" + PATH); 
     final ObjectMapper mapper = new ObjectMapper(); 
     final JsonNode tmp = mapper.readTree(in); 

    //replacing the value 
    ObjectNode.class.cast(tmp).get("url").get(0).get("path").toString().replace(DEFAULT_ENDPOINT,configuration.getApiEndPoint()); 

    //write to the file 
    byte[] contentAsByte = tmp.toString().getBytes(); 
    final FileOutputStream out = new FileOutputStream(System.getProperty("user.dir") + "/" + PATH); 
    out.write(contentAsByte); 
    out.flush(); 
    out.close(); 
    in.close();` 

答えて

0

使用している.replace()メソッドは、JsonNodeからではなく、Stringからのものです。 JsonNode変更する

使用この:

// replacing the value 
    ObjectNode obj = ObjectNode.class.cast(tmp.get("url").get(0)); 
    obj.set("path", new TextNode("newValue")); 
+0

は提案をいただき、ありがとうございます。私はそれを試みたが、うまくいかなかった。私は以下を追加して解決することができます。 ObjectNode.class.cast(ObjectNode.class.cast(tmp).get( "url")。get(0).get( "path"))put(DEFAULT_ENDPOINT、configuration.getApiEndPoint()); –

関連する問題