You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用org.json包修改JSON值并写回原文件?

使用org.json修改汇率JSON文件的步骤

核心说明

org.json里的JSONObject.put()方法既能新增键值对,也能替换已存在的键值——这正是你需要的功能,不用找额外的函数。

具体实现步骤

  1. 取出JSONArray内的JSONObject
    你的desiredJsonArray里只有一个JSONObject元素,直接用索引0取出:

    JSONObject ratesObj = desiredJsonArray.getJSONObject(0);
    
  2. 修改指定键的数值
    用put()方法直接覆盖原有值,比如修改euro对应的yen、gbp、usd:

    // 示例:修改yen为157.0,gbp为0.86,usd为1.09
    ratesObj.put("yen", 157.0);
    ratesObj.put("gbp", 0.86);
    ratesObj.put("usd", 1.09);
    

    注:因为desiredJsonArray持有对这个JSONObject的引用,修改后JSONArray的内容会自动更新,不需要额外放回操作。

  3. 将修改后的JSON写回原文件
    把顶层的jsonObject转成字符串,再写入文件。如果需要格式化输出(方便阅读),可以用toString(4)指定缩进:

    // 转成格式化的JSON字符串
    String updatedJson = jsonObject.toString(4);
    // 写回原文件
    Files.write(Paths.get("src/rates.json"), updatedJson.getBytes(StandardCharsets.UTF_8));
    

完整整合代码

把上述步骤和你已有的代码整合起来:

import org.json.JSONArray;
import org.json.JSONObject;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;

public class RateUpdater {
    public static void main(String[] args) throws Exception {
        // 1. 加载JSON文件到JSONObject
        String jsonContent = new String(Files.readAllBytes(Paths.get("src/rates.json")));
        JSONObject jsonObject = new JSONObject(jsonContent);

        // 2. 找到缺失的币种(你的原有代码)
        String requiredCurrency = null;
        String[] currencies = new String[] {"usd", "gbp", "euro", "yen"};
        // 这里假设gui.orderOfExchangesForConfirmRateChanges是你的ArrayList
        for(String currency : currencies){
            if(!gui.orderOfExchangesForConfirmRateChanges.contains(currency)){
                requiredCurrency = currency;
                System.out.println("Found currency: " + requiredCurrency);
                break; // 找到后跳出循环,避免覆盖
            }
        }

        if(requiredCurrency != null) {
            // 3. 获取对应JSONArray
            JSONArray desiredJsonArray = jsonObject.getJSONArray(requiredCurrency);
            // 4. 取出内部的汇率JSONObject
            JSONObject ratesObj = desiredJsonArray.getJSONObject(0);

            // 5. 修改目标汇率值(这里替换成你的指定数值)
            if("euro".equals(requiredCurrency)) {
                ratesObj.put("yen", 157.0);
                ratesObj.put("gbp", 0.86);
                ratesObj.put("usd", 1.09);
            }
            // 可以添加其他币种的修改逻辑
            else if("usd".equals(requiredCurrency)) {
                ratesObj.put("yen", 145.0);
                ratesObj.put("gbp", 0.78);
                ratesObj.put("euro", 0.91);
            }

            // 6. 写回原文件
            String updatedJson = jsonObject.toString(4);
            Files.write(Paths.get("src/rates.json"), updatedJson.getBytes(StandardCharsets.UTF_8));
            System.out.println("汇率已更新并写入文件");
        } else {
            System.out.println("未找到缺失的币种");
        }
    }
}

注意事项

  • 记得处理Exception(代码里用了throws Exception,实际项目中可以更精细地捕获IO异常和JSON解析异常)
  • 原有找缺失币种的代码里,建议用!gui.orderOfExchangesForConfirmRateChanges.contains(currency)替代toString().contains(),避免字符串误判
  • 循环找币种时加break,防止找到多个币种时覆盖结果

内容的提问来源于stack exchange,提问作者wyznel

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.16 08:38:13