我尝试解析此JSON地址,但失败了。我尝试的代码失败,没有数据反映。你能帮我吗?
我的代码;
private void parseJSON() {
final String url = "json website";
JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONArray jsonArray = response.getJSONArray(null);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject hit = jsonArray.getJSONObject(i);
String imageref = hit.getString(null);
mExampleList.add(new ExampleItemRef(imageref));
}
此JSON;
[
"https://www.asdasd.com/images/r/a.png",
"https://www.asdasd.com/images/r/b.png",
"https://www.asdasd.com/images/r/c.png",
"https://www.asdasd.com/images/r/d.png",
"https://www.asdasd.com/images/r/e.png"
]
答案 0 :(得分:0)
只需从代码中删除所有不必要的部分,然后按如下所示进行更改即可。创建数组时需要放置json字符串
private void parseJSON() {
final String url = "json website";
JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONArray jsonArray = response.getJSONArray(jsonString);
for (int i = 0; i < jsonArray.length(); i++) {
mExampleList.add(new ExampleItemRef(jsonArray.getString(i)));
}
}
答案 1 :(得分:0)
您可以这样:
ArrayList < ExampleItemRef > mExampleList = new ArrayList <ExampleItemRef > ();
String jsonStr =
"[ \"https://www.asdasd.com/images/r/a.png\", \"https://www.asdasd.com/images/r/b.png\", \"https://www.asdasd.com/images/r/c.png\", \"https://www.asdasd.com/images/r/d.png\", \"https://www.asdasd.com/images/r/e.png\" ]";
try {
JSONArray jsonArray = new JSONArray(jsonStr);
for (int i = 0; i < jsonArray.length(); i++) {
String imageref = jsonArray.getString(i);
mExampleList.add(new ExampleItemRef(imageref));
}
} catch (JSONException e) {
e.printStackTrace();
}
代码是自我解释的,因此没有注释,希望对您有所帮助
已编辑:尝试此操作
private void parseJSON() {
final String url = "json website";
JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONArray jsonArray = new JSONArray(response.toString());
for (int i = 0; i < jsonArray.length(); i++) {
String imageref = jsonArray.getString(i);
mExampleList.add(new ExampleItemRef(imageref));
}