0

JSON file: [
{
"name":"John", "city":"Berlin", "job":"Teacher" }, {
"name":"Mark", "city":"Oslo", "job":"Doctor" } ]

JSONParser parser = new JSONParser(); JSONArray a = (JSONArray) parser.parse(new FileReader("F:\file.json"));

    for (Object o : a) {
        JSONObject person = (JSONObject) o;

        String name = (String) person.get("name");
        
        if (name.equalsIgnoreCase("john")) {
            String name1 = (String) person.get("name");
            System.out.println("name1" + name1);

            String city1 = (String) person.get("city");
            System.out.println("city1" + city1);

            String job1 = (String) person.get("job");
            System.out.println("job1" + job1);  
            
            person.put("city", "BLR");
            String city2 = (String) person.get("city");
            System.out.println("city2" + city2);
        }
    }

Value are not updating in Json external file

1
  • Save the json file for it to get updated, you just changed the value of an object not in the local file Commented Jun 18, 2021 at 11:59

1 Answer 1

0

JSONObject is a represent of json that does not link to originally file. To rewrite file you need to change values in person and write it to file.

person.put("city", "BLR");
String jsonString = person.toString();
// write jsonString to file
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.