Android 为什么我能';无法在json中访问此数组
我正试图在下面的json文件中获得这个图像数组,并在GridView中显示它,但我不知道为什么我不能达到这个目的Android 为什么我能';无法在json中访问此数组,android,arrays,json,Android,Arrays,Json,我正试图在下面的json文件中获得这个图像数组,并在GridView中显示它,但我不知道为什么我不能达到这个目的 [ { "id": 385, "title": "this title", "image": "logo.png", "description": "description ", "images": [ "logo.png", "image1.png" ], "user_name": "my name
[
{
"id": 385,
"title": "this title",
"image": "logo.png",
"description": "description ",
"images": [
"logo.png",
"image1.png"
],
"user_name": "my name",
"telephone": "0123456789",
"city_name": "جدة",
"created_at": "2017-03-20T17:51:16+00:00",
"time_ago": "6 days ago",
"user_id": 159
}
]
这是我的全部任务
private class AysncImages extends AsyncTask<String, Void, Integer> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// Showing progress dialog
// pDialog = new ProgressDialog(getActivity());
// pDialog.setMessage("Please wait...");
// pDialog.setCancelable(false);
// pDialog.show();
}
@Override
protected Integer doInBackground(String... arg0) {
// Building Parameters
HttpHandler sh = new HttpHandler();
String jsonStr = sh.makeServiceCall(String.valueOf(Config.BASE_URL));
//jsonStr = sh.makeServiceCall(url + 373); //or url + ID
Log.i(TAG, "doInBackground Image: " + jsonStr);
if (jsonStr != null) {
try
{
JSONObject jsonObj = new JSONObject(jsonStr);
JSONArray arrayObj = jsonObj.getJSONArray("images");
Log.i(TAG, "array: "+ jsonObj);
GridItem item = new GridItem();
for (int i = 0; i < arrayObj.length(); i++) {
String images = arrayObj.getString(i);
Log.i(TAG, "parseResult: " + jsonObj);
item.setImage(IMAGE_LINK + images);//IMAGE_LINK +
Log.i(TAG, "parseResult: " + IMAGE_LINK + " " + images);
mGridData.add(item);
}
// Getting JSON Array node
} catch (JSONException e1) {
e1.printStackTrace();
}
}
return null;
}
@Override
protected void onPostExecute(Integer result) {
// if (result == 1) {
mGridAdapter.setGridData(mGridData);
//} else {
// Toast.makeText(getActivity(), "Failed to fetch data!", Toast.LENGTH_SHORT).show();
// }
//Hide progressbar
mProgressBar.setVisibility(View.GONE);
}
}
私有类AysncImages扩展异步任务{
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
//显示进度对话框
//pDialog=newprogressdialog(getActivity());
//setMessage(“请稍候…”);
//pDialog.setCancelable(假);
//pDialog.show();
}
@凌驾
受保护的整数doInBackground(字符串…arg0){
//建筑参数
HttpHandler sh=新的HttpHandler();
String jsonStr=sh.makeServiceCall(String.valueOf(Config.BASE_URL));
//jsonStr=sh.makeServiceCall(url+373);//或url+ID
Log.i(标签,“doInBackground图像:”+jsonStr);
if(jsonStr!=null){
尝试
{
JSONObject jsonObj=新的JSONObject(jsonStr);
JSONArray arrayObj=jsonObj.getJSONArray(“图像”);
Log.i(标记,“数组:”+jsonObj);
GridItem=新的GridItem();
对于(int i=0;i
在我的日志中,我无法访问jsonObj
或arrayObj
我只能访问jsonStr
我缺少什么
我的jsonStr日志
试试这个方法
try {
JSONArray arrayObj = new JSONArray(jsonStr);
for (int i = 0; i < arrayObj.length(); i++) {
JSONObject jsonObject = arrayObj.getJSONObject(i);
JSONArray arrayObjImage = jsonObject.getJSONArray("images");
String image1 = arrayObjImage.getString(0);
String image2 = arrayObjImage.getString(1);
}
} catch (JSONException e1) {
e1.printStackTrace();
}
试试看{
JSONArray arrayObj=新JSONArray(jsonStr);
对于(int i=0;i
试试这个
try {
JSONArray jArr = new JSONArray(jsonStr);
for (int i = 0; i < jArr.length(); i++) {
JSONObject Obj = jArr.getJSONObject(i);
String id=Obj.getString("id");
String title=Obj.getString("title");
String description=Obj.getString("description");
JSONArray image_arr=Obj.getJSONArray("images");
for (int j = 0; j < image_arr.length(); j++) {
String value=image_arr.getString(j);
Log.d("TAG","value:"+value);
}
}
} catch (JSONException e1) {
e1.printStackTrace();
}
试试看{
JSONArray jArr=新JSONArray(jsonStr);
for(int i=0;i
首先,查看您的Json定义:
[
{
"id": 385,
"title": "this title",
...
}
]
还可以看看您是如何在代码中访问Json的:
String jsonStr = sh.makeServiceCall(String.valueOf(Config.BASE_URL));
JSONObject jsonObj = new JSONObject(jsonStr);
JSONArray arrayObj = jsonObj.getJSONArray("images");
在代码中,jsonObj
应该是json的根对象,但是如果仔细查看,json没有根节点。在根级别,Json是一个数组。这就是为什么您不能访问jsonObj
,因此,这就是为什么您不能访问images
数组
要解决此问题,您有两个选项:
1) 通过删除[
和]
使服务器返回对象而不是数组。例如
{
"id": 385,
"title": "this title",
...
}
2) 将代码更改为开始读取根作为数组的第一个位置。例如:
String jsonStr = sh.makeServiceCall(String.valueOf(Config.BASE_URL));
JSONArray jsonArray = new JSONArray(jsonStr);
JSONObject jsonObj = jsonArray.get(0);
JSONArray arrayObj = jsonObj.getJSONArray("images");
(作为旁注:我已经有一段时间没有用Android编程了,所以我不确定上面代码的正确性)
如果您可以更改服务器,最好选择选项1。发布完整的json响应,我没有看到任何数组标记名:
images
您的json不可用vaild@rafsanahmad007请检查我的json是否有图像:[“logo.png”,“image1.png”]
完整的json响应应该在JsonObject中,并将您的完整json发布到json的样子?