我有一个如下所示的 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","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);
}
我想从第二个 JSONObject 传递值并将它们添加到 HashMap 并随后添加到 Arraylist,这样我就可以将它们传递给另一个 Fragment .. 这是最好的方法吗?或者我如何最好地将这个值传递给另一个片段?