Using the org.json java package, how would I replace the values of certain json values, and then put them back into the main file?

57 views Asked by At

I'm relatively new to Java, and I'm struggling to find any "easy to understand" documentation on how to properly use the org.json package, and I can't seem to find any function that replaces the value of a key, only adding new ones.

By the way, sorry if this post isn't properly formatted or something, am also new to stackoverflow.

This is the content of my JSON file:

{
  "gbp": [
    {
      "yen": 183.65,
      "usd": 1.28,
      "euro": 1.17
    }
  ],
  "usd": [
    {
      "yen": 144.67,
      "gbp": 0.79,
      "euro": 0.92
    }
  ],
  "euro": [
    {
      "yen": 156.96,
      "usd": 1.08,
      "gbp": 0.85
    }
  ],
  "yen": [
    {
      "euro": 0.0064,
      "usd": 0.0069,
      "gbp": 0.0054
    }
  ]
}

This is the code that loads the jsonContent into a string then an object:

String jsonContent = new String(Files.readAllBytes(Paths.get("src/rates.json")));
JSONObject jsonObject = new JSONObject(jsonContent);

I then need to find the missing currency in an ArrayList named: orderOfExchangesForConfirmRateChanges. This is in a separate java file, so it is called with: gui.orderOfExchangesForConfirmRateChanges. This is done with this code:

            String requiredCurrency = null; //this is needed for later
            String[] currencies = new String[] {"usd", "gbp", "euro", "yen"};
            for(String currency : currencies){
                if(!gui.orderOfExchangesForConfirmRateChanges.toString().contains(currency)){
                    requiredCurrency = currency;
                    System.out.println("Found currency.");
                }
            }

Once this has completed, we call this bit of code to get the data within the json file corresponding to the requiredCurrency variable:

JSONArray desiredJsonArray = jsonObject.getJSONArray(requiredCurrency);
System.out.println(desiredJsonArray); // here to help me visualise things - example, if requiredCurrency = euro, output would be: [{"yen":156.96,"gbp":0.85,"usd":1.08}]

So hereafter, I need to change the values of "yen", "gbp", "usd", but I cannot seem to figure it out. For example if I wanted to change the contents of the euro section of rates.json from:

{
   "euro": [
       {
      "yen": 144.67,
      "gbp": 0.79,
      "euro": 0.92
       }
  ]
}

to:

{
   "euro": [
       {
      "yen": 2000,
      "gbp": 50,
      "euro": 100
       }
  ]
}

And then re-insert it into the main JSONObject and rewrite the rates.json file to include these new values.

Sorry if this question is a bit all over the place.

If anything needs to be cleared up or re-explained, let me know

1

There are 1 answers

0
marcinj On

I think you almost had it.

In your json structure, the value for each currency is an array which contains a single object. To change a value of a specific currency inside an array, you need to get the JSONObject from the array then change the value.

// This is from your code
JSONArray jsArray = jsonObject.getJSONArray("gbp");
JSONObject desiredJsonArray = jsArray.getJSONObject(0);

// This is the update part
desiredJsonArray.put("yen", 155);
desiredJsonArray.put("gbp", 1);
desiredJsonArray.put("usd", 2);

now, write json back to file:

Files.write(Paths.get("src/rates.json"), jsonObject.toString(4).getBytes());