gpt4 book ai didi

java - JsonObject 类型中的方法 add(String, JsonElement) 不适用于参数 (String, String)

转载 作者:行者123 更新时间:2023-12-01 11:12:23 25 4
gpt4 key购买 nike

我正在尝试使用 json.org 库将 string 转换为 JsonObject 但出现此错误:

The method add(String, JsonElement) in the type JsonObject is not applicable for the arguments (String, String)

代码:

String macAddress = wInfo.getMacAddress();  
JsonObject jsonObject = new JsonObject();
jsonObject.add("mac", macAddress);

结果应如下所示:{“mac”:“10:A5:D0:06:C6:E9”}

最佳答案

要获得所需的结果{"mac": "10:A5:D0:06:C6:E9"},请使用put而不是add

JSONObject jsonObject = new JSONObject();
jsonObject.put("mac", macAddress);

而不是

jsonObject.add("mac", macAddress);

关于java - JsonObject 类型中的方法 add(String, JsonElement) 不适用于参数 (String, String),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32200677/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com