错误:
org.json.JSONException:java.lang.String类型的值[{“username”:“ghs”}]无法转换为JSONObject W / System.err:at org.json.JSON.typeMismatch(JSON.java:111)
Android端代码:
$ g++ --version
g++ (GCC) 6.2.1 20160830
Copyright (C) 2016 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
$ clang++ --version
clang version 3.9.0 (tags/RELEASE_390/final)
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /usr/bin
$ g++ -std=c++11 order_of_ops.cpp -o a.out && ./a.out; echo $?
27
$ clang++ -std=c++11 order_of_ops.cpp -o a.out && ./a.out; echo $?
81
php结束:
public void listdrawer(){
onlineList_uname = new ArrayList<>();
try {
JSONObject jsonResponse = new JSONObject(jsonResult);
JSONArray jsonMainNode = jsonResponse.getJSONArray("username");
for (int i = 0; i < jsonMainNode.length(); i++) {
JSONObject jsonChildNode = jsonMainNode.getJSONObject(i);
String name = jsonChildNode.optString("username");
onlineList_uname.add(i,name);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
答案 0 :(得分:1)
如果您的jsonResult = [{"username":"g4"},{"username":"ghs"},{"username":"g"},{"username":"e"},{"username":"a"}]
<强>代码:强>
ArrayList<String> onlineList_uname = new ArrayList<>();
String jsonResult = "[{\"username\":\"g4\"},{\"username\":\"ghs\"},{\"username\":\"g\"},{\"username\":\"e\"},{\"username\":\"a\"}]"; // = [{"username":"g4"},{"username":"ghs"},{"username":"g"},{"username":"e"},{"username":"a"}]
try {
JSONArray jsonResponse = new JSONArray(jsonResult);
for (int i = 0; i < jsonResponse.length(); i++) {
JSONObject jObj = jsonResponse.getJSONObject(i);
String name = jObj.getString("username");
onlineList_uname.add(i, name);
}
for (int j = 0; j < onlineList_uname.size(); j++) {
Log.i("LOG", "username#" + j + " " + onlineList_uname.get(j));
}
} catch (JSONException e) {
e.printStackTrace();
}
结果:
用户名#0 g4
用户名#1 ghs
用户名#2 g
用户名#3 e
用户名#4 a
答案 1 :(得分:0)
尝试:
jsonChildNode.optString("username").toString();
我认为之前我遇到过同样的问题,并且toString方法解决了它。