我有一个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中,这样我就可以将它们传递给另一个片段..这是最好的方法吗?或者我如何才能将这些值最好地传递给另一个片段?
答案 0 :(得分:0)
使用Fragment.setArguments()
传递Bundle实例。在Bundle中,您可以设置Serializable
对象(例如Collection
HashMap
答案 1 :(得分:0)
您必须首先创建一个Bundle,然后将可序列化的hashmap添加到bundle中,然后设置片段的参数如下所示:
Bundle args = new Bundle();
args.putSerializable('key', 'value');
Fragment fragment = new Fragment();
fragment.setArguments(args);