问题
I am trying to convert a string to JsonObject with the json.org libraray but I am getting this error:
The method add(String, JsonElement) in the type JsonObject is not applicable for the arguments (String, String)
Code:
String macAddress = wInfo.getMacAddress();
JsonObject jsonObject = new JsonObject();
jsonObject.add("mac", macAddress);
Result should look such as the following: {"mac": "10:A5:D0:06:C6:E9"}
回答1:
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);
instead of
jsonObject.add("mac", macAddress);
回答2:
I think I am bit late, but this answer might help others. You can have your code like below
jsonObject.add("mac", String.valueOf(macAddress));
Instead of using"put()" than "add()".
回答3:
You can use Gson
jsonObj.add("mac", new Gson().toJsonTree(macAddress));
来源:https://stackoverflow.com/questions/32200677/the-method-addstring-jsonelement-in-the-type-jsonobject-is-not-applicable-for