为什么我有两个'[['? 应该是一个括号吗? '['
[[{"name":"50 shade of gray","path":"http://api.androidhive.info/json/movies/1.jpg","description":"long story","likes":"5","comments":" Very nice"},{"name":"Transformers","path":"http://api.androidhive.info/json/movies/2jpg","description":"Robots","likes":"7","comments":" beautiful"},{"name":"Avangeers","path":"http://api.androidhive.info/json/movies/3.jpg","description":"hulk and iron man","likes":"8","comments":" wow"}]]
这是我的php
$sql = "select name , path , description , likes , comments from image ";
$res = mysqli_query($con,$sql);
$result = array();
while($row = mysqli_fetch_array($res)){
array_push($result,
array('name'=>$row[0],
'path'=>$row[1],
'description'=>$row[2],
'likes'=>$row[3],
'comments'=>$row[4]
));
}
echo json_encode(array($result));
mysqli_close($con);
在这样的代码中获取它是否正确?
JsonArrayRequest movieReq = new JsonArrayRequest(url,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
Log.d("r", response.toString());
hidePDialog();
System.out.print(response);
// Parsing json
for (int i = 0; i < response.length(); i++) {
try {
JSONObject obj = response.getJSONObject(i);
Movie movie = new Movie();
movie.setTitle(obj.getString("name"));
movie.setThumbnailUrl(obj.getString("path"));
movie.setRating(obj.getString("description"));
// movie.setRating(((Number) obj.get("description"))
// .doubleValue());
movie.setYear(obj.getString("likes"));
// movie.setYear(obj.getInt("likes"));
// Genre is json array
JSONArray genreArry = obj.getJSONArray("comments");
ArrayList<String> genre = new ArrayList<String>();
for (int j = 0; j < genreArry.length(); j++) {
genre.add((String) genreArry.get(j));
}
movie.setGenre(genre);
// adding movie to movies array
movieList.add(movie);
} catch (JSONException e) {
e.printStackTrace();
}
}
// notifying list adapter about data changes
// so that it renders the list view with updated data
adapter.notifyDataSetChanged();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
System.out.print(error);
VolleyLog.d(TAG, "Error: " + error.getMessage());
hidePDialog();
}
});
最佳答案
$result
是一个数组。
您在该数组中推送关联数组(成为对象),所以现在您有 [{...},{...}]
然后你做:
echo json_encode(array($result));
所以你得到一个数组,里面有一个数组,导致上面的结果带有额外的'[':[[{...},{...}]]
关于android - 为什么我在 json 中有双括号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31722468/