java读取json文件并转化为JSONObject,并修改json的value值,问题总结

public boolean putPipelineConfig(String pipelineId) {

    boolean result = false;
    try {
        Header[] headers = null;
        String input = FileUtils.readFileToString(new File("src/test/resources/pipelineConfig.json"), "UTF-8");     //需要maven依赖
        net.sf.json.JSONObject jsonObject = net.sf.json.JSONObject.fromObject(input);     //需要maven依赖
        //System.out.println(jsonObject);

        jsonObject.put("id", pipelineId);    //修改默认json文件里的value
        //System.out.println(jsonObject);

        String url = Constants.PIPELINE_API;
        if (!url.endsWith(pipelineId))
            url += pipelineId;
        HttpResponse response = HttpRequest.Put(TestBase.hostPort + url,
                TestBase.cookie,
                headers,
                jsonObject.toString());
        int responseCode = response.getStatusLine().getStatusCode();
        result = responseCode == 204 ? true : false;

    } catch (Exception e) {
        System.out.println(methodName + ": failed with Exception as:" + e);
        e.printStackTrace();
        Assert.fail("failed with Exception as:" + e);

    }
    return result;
}

1.需要maven依赖

<dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.4</version>
</dependency>
<dependency>
            <groupId>net.sf.json-lib</groupId>
            <artifactId>json-lib</artifactId>
            <version>2.4</version>
            <classifier>jdk15</classifier>
</dependency>

2.import导包:import net.sf.json.JSONObject;

但是,import net.sf.json.JSONObject;与import org.json.simple.JSONObject;同时出现会报错:

'org.json.simple.JSONObject' is already defined in a single-type import

解决方法:

不导入net.sf.json.JSONObject包,在使用时:(依赖依然需要)

net.sf.json.JSONObject jsonObject = net.sf.json.JSONObject.fromObject(input);

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值