JsonObject类型的方法add(String,JsonElement)不适用于参数(String,String) [英] The method add(String, JsonElement) in the type JsonObject is not applicable for the arguments (String, String)
本文介绍了JsonObject类型的方法add(String,JsonElement)不适用于参数(String,String)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用json.org
libraray将string
转换为JsonObject
,但出现此错误:
I am trying to convert a string
to JsonObject
with the json.org
libraray but I am getting this error:
类型为JsonObject的方法add(String,JsonElement)不适用于参数(String,String)
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"}
Result should look such as the following: {"mac": "10:A5:D0:06:C6:E9"}
推荐答案
要获得所需的结果{"mac": "10:A5:D0:06:C6:E9"}
,请使用put
而不是add
to get the required result {"mac": "10:A5:D0:06:C6:E9"}
, use put
instead of add
JSONObject jsonObject = new JSONObject();
jsonObject.put("mac", macAddress);
代替
jsonObject.add("mac", macAddress);
这篇关于JsonObject类型的方法add(String,JsonElement)不适用于参数(String,String)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文