我无法读取json数据并在andord.list视图中绑定到列表视图,但是doctype的值始终显示为v4.could你能告诉我我在code.i附带了解错的代码json数据。
[
{
id: "HT20140109105317640",
basePath: "D:\img",
lstDocType: [
{
doctype: "PP1",
imageName: "CH100610_002_003_PP1.jpg",
imageupload: false
},
{
doctype: "PP2",
imageName: "CH100610_002_003_PP2.jpg",
imageupload: false
},
{
doctype: "V1",
imageName: "CH100610_002_003_V1.jpg",
imageupload: false
},
{
doctype: "V2",
imageName: "CH100610_002_003_V2.jpg",
imageupload: false
},
{
doctype: "V3",
imageName: "CH100610_002_003_V3.jpg",
imageupload: false
},
{
doctype: "V4",
imageName: "CH100610_002_003_V4.jpg",
imageupload: false
}
],
f_FNAME: "SANJAY",
f_FORM: "002",
f_TCD: "CH100610",
f_TNO: "003",
f_LNAME: "NANKANI"
},
{},
{},
{},
{},
{},
{},
{},
{},
{}
]
这是解析json数据的代码
public class DisplayList extends AsyncTask<String, String, String>
{
@Override
protected void onPreExecute()
{
// TODO Auto-generated method stub
super.onPreExecute();
pd=new ProgressDialog(MainActivity.this);
pd.setMessage("Please Wait List Is Loading");
pd.setCancelable(false);
pd.show();
}
@Override
protected String doInBackground(String... params)
{
// TODO Auto-generated method stub
try
{
// create instance of json data class
TourList tl=new TourList();
jarray=tl.getJsonFromUrl("http://192.168.1.239:8080/VisaDocUpload/historycon/selectbycount/0/10");
for(int i=0;i<jarray.length();i++)
{
HashMap<String,String> map=new HashMap<String,String>();
JSONObject c=jarray.getJSONObject(i);
id=c.getString(KEY_ID);
JSONArray doct=c.getJSONArray("lstDocType");
for(int j=0;j<doct.length();j++)
{
JSONObject jo=doct.getJSONObject(j);
doctype=jo.getString(KEY_DOCTYPE);
}
map.put(KEY_ID,id);
map.put(KEY_DOCTYPE,doctype);
arraylist.add(map);
}
}
catch(Exception e)
{
Log.d("In Back","");
}
return null;
}
@Override
protected void onPostExecute(String result)
{
// TODO Auto-generated method stub
super.onPostExecute(result);
pd.dismiss();
lv.setAdapter(new MyTourDisplay(MainActivity.this,arraylist));
Toast.makeText(getApplicationContext(), ""+id+doctype,Toast.LENGTH_LONG).show();
}
}
答案 0 :(得分:1)
解析是正确的,问题与你只是在更多内部for循环中分配doctype这一事实有关,因此,在地图中,你将最后一个赋值给doctype
private class Holder {
public ArrayList<String>() docLst;
// possible more kind of elements
}
然后:
Holder holder = new Holder();
holder.docLst = new ArrayList<String>();
for(int j=0;j<doct.length();j++) {
JSONObject jo=doct.getJSONObject(j);
doctype=jo.getString(KEY_DOCTYPE);
holder.docLst.add(doctype);
}
map.put(id, holder);
当然,您的HashMap必须属于<String, Holder>
答案 1 :(得分:0)
使用这种JSON格式:
{
"array" : [
{
id: "HT20140109105317640",
basePath: "D:\img",
lstDocType: [
{
doctype: "PP1",
imageName: "CH100610_002_003_PP1.jpg",
imageupload: false
},
{
doctype: "PP2",
imageName: "CH100610_002_003_PP2.jpg",
imageupload: false
}
]
}
]
}
然后继续这样:
json=tl.getJsonFromUrl("http://192.168.1.239:8080/VisaDocUpload/historycon/selectbycount/0/10");
JSONObject jsonObj = new JSONObject(json);
JSONArray jarray = jsonObj.getJSONArray("array");
for(int i=0;i<jarray.length();i++)
{
//exec your code here
}
我不确定这是否适用于imageupload:假没有引号。