Java 通过嵌套JSON对象循环嵌套并将值保存在数组列表中
我有一个JSON对象,如下所示Java 通过嵌套JSON对象循环嵌套并将值保存在数组列表中,java,android,json,Java,Android,Json,我有一个JSON对象,如下所示 {"PAYLOAD":[{"pid":"4","title":"Kyamaiko Flats","long":"36.764880000","lat":"-1.342980800","volume":125,"value":"10000","active":"Y","contractor":"SS mehta and Sons","subContractor":"Kamau Njoro","cp":[{"contactPerson":"Njoroge","desi
{"PAYLOAD":[{"pid":"4","title":"Kyamaiko Flats","long":"36.764880000","lat":"-1.342980800","volume":125,"value":"10000","active":"Y","contractor":"SS mehta and Sons","subContractor":"Kamau Njoro","cp":[{"contactPerson":"Njoroge","designation":"Architect"},{"contactPerson":"John","designation":"Quantity suveyor"}],"lastvisit":"2 months ago ","nextvisit":"10/12/2013 ""image_url":"http:\\www.someurl.net\images\avator.png"}]}
要获得这些值,我使用以下代码:
try{
JSONArray mot = json.getJSONArray("PAYLOAD");
for(int i=0;i<mot.length();i++){
HashMap<String, String> map = new HashMap<String, String>();
JSONObject e = mot.getJSONObject(i);
map.put(KEY_NAME_ID, e.getString(KEY_NAME_ID));
map.put(KEY_NAME, e.getString(KEY_NAME));
map.put(KEY_LAT, e.getString(KEY_LAT));
map.put(KEY_LON, e.getString(KEY_LON));
map.put(KEY_VOLUME, e.getString(KEY_VOLUME));
map.put(KEY_VALUE, e.getString(KEY_VALUE));
map.put(KEY_ACCOUNT_STATUS, e.getString(KEY_ACCOUNT_STATUS));
map.put(KEY_CONTRACTOR, e.getString(KEY_CONTRACTOR));
map.put(KEY_SUB_CONTRACTOR, e.getString(KEY_SUB_CONTRACTOR));
JSONArray cp = json.getJSONArray(KEY_CONTACT_PERSON);
for(int j=0;j<cp.length();j++)
{
HashMap<String, String> map_cp = new HashMap<String, String>();
JSONObject f = cp.getJSONObject(j);
map_cp.put(KEY_CONTACT_PERSON_NAME, f.getString(KEY_CONTACT_PERSON_NAME));
map_cp.put(KEY_CONTACT_PERSON_DESIGNATION, f.getString(KEY_CONTACT_PERSON_DESIGNATION));
}
map.put(KEY_LAST_VISIT, e.getString(KEY_LAST_VISIT));
map.put(KEY_NEXT_VISIT, e.getString(KEY_NEXT_VISIT));
map.put(KEY_IMAGE_URL, e.getString(KEY_IMAGE_URL));
displaylist.add(map);
}
}catch(JSONException e) {
Util.LogFailedVisits ("JSON Exception "+e.toString(),mDID);
}
试试看{
JSONArray mot=json.getJSONArray(“有效载荷”);
例如(int i=0;i使用Fragment.setArguments()
来传递一个Bundle实例。在Bundle中,您可以放置一个可序列化的对象(例如,您的HashMap
的集合)您必须首先创建一个Bundle,然后将可序列化的hashmap添加到Bundle中,然后设置片段的参数,如下所示:
Bundle args = new Bundle();
args.putSerializable('key', 'value');
Fragment fragment = new Fragment();
fragment.setArguments(args);
如果我这样做,那么我会得到错误:类型片段中的方法setArguments(Bundle)不适用于参数(ArrayList)