如何替换json文件中的值

Lit*_*ird 7 java json

我有一个 JSON 文件,即test.json.

{
  "Added": {
    "type": "K",
    "newmem": {
      "IDNew": {
        "id": "777709",
        "type": "LOP"
      },
      "birthDate": "2000-12-09"
    },
    "code": "",
    "newest": {
      "curlNew": "",
      "addedForNew": ""
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

我尝试了以下代码:

File file = new File("test.json");
JSONParser parser = new JSONParser();

JSONObject data =  (JSONObject) parser.parse(
   new FileReader(file.getAbsolutePath()
));//path to the JSON file.
System.out.println(data.toString());

JSONObject jObject  = new JSONObject();
jObject.put("id","12345678");
System.out.println(jObject);
Run Code Online (Sandbox Code Playgroud)

结果得到:-

{
  "Added": {
    "type": "K",
    "newmem": {
      "IDNew": {
        "id": "777709",
        "type": "LOP"
      },
      "birthDate": "2000-12-09"
    },
    "code": "",
    "newest": {
      "curlNew": "",
      "addedForNew": ""
    }
  }
}{
"id":"12345678"
}
Run Code Online (Sandbox Code Playgroud)

id:“777709”没有更新到id:“12345678”,但它终于添加了。请帮助我并告诉我如何替换该id值。

flo*_*der 10

您可以使用简单的 json library( library )尝试此操作。我单独打印所有对象以供理解。因为你在另外两个对象中声明了 Id 对象,所以首先你必须得到这个对象,然后得到你想要的对象 IDNew。然后在 id 字段中放入新的 id 值。

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Iterator;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

public class Main {

    private static final String filePath = "E:\\project-test\\scloud\\test\\src\\main\\resources\\test";

    public static void main(String[] args) {

        try {
            // read the json file
            FileReader reader = new FileReader(filePath);

            JSONParser jsonParser = new JSONParser();
            JSONObject jsonObject = (JSONObject) jsonParser.parse(reader);

            System.out.println(jsonObject);


            JSONObject addedObj = (JSONObject) jsonObject.get("Added");
            System.out.println("Added is: " + addedObj);

            JSONObject newmemObject =(JSONObject) addedObj.get("newmem");
            System.out.println("newmemObject is: " + newmemObject);

            JSONObject idNewObj =(JSONObject) newmemObject.get("IDNew");
            System.out.println("IdNewObj is: " + idNewObj);

            long id =Long.valueOf((String) idNewObj.get("id"));
            System.out.println(id);


            idNewObj.put("id",809809809);

            System.out.println(jsonObject);

        } catch (FileNotFoundException ex) {
            ex.printStackTrace();
        } catch (IOException ex) {
            ex.printStackTrace();
        } catch (ParseException ex) {
            ex.printStackTrace();
        } catch (NullPointerException ex) {
            ex.printStackTrace();
        }

    }

}
Run Code Online (Sandbox Code Playgroud)

或者为简单起见,您可以使用它

    FileReader reader = new FileReader(filePath);
    JSONParser jsonParser = new JSONParser();
    JSONObject jsonObject = (JSONObject) jsonParser.parse(reader);
    System.out.println(jsonObject);

    JSONObject idObj = (
       (JSONObject) (
             (JSONObject) (
                (JSONObject)
                   jsonObject.get("Added")
             ).get("newmem")
       ).get("IDNew")
    );

    idObj.put("id", 98009809);
    System.out.println("After ID value updated : "+jsonObject);
Run Code Online (Sandbox Code Playgroud)

  • 试试这个: FileOutputStream outputStream = new FileOutputStream("test"); byte[] strToBytes = jsonObject.toString().getBytes(); outputStream.write(strToBytes); (3认同)

tay*_*tos 6

您可以JSONObject使用 simple-json java lib更新 a 中的嵌套元素,如下所示:

JSONObject added = (JSONObject) data.get("Added");
JSONObject newmem = (JSONObject) added.get("newmem");
JSONObject idNew = (JSONObject) newmem.get("IDNew");
idNew.put("id","12345678");
System.out.println(data);
Run Code Online (Sandbox Code Playgroud)