[英]Looping nested through a nested JSON Object and saving values in an array list
I have a JSON object as the one shown below 我有一个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"}]}
To get the Values I use the code below: 要获取值,请使用以下代码:
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);
}
I want to Pass the values from the second JSONObject and add them to a HashMap and subsequentlty to an Arraylist so I can be able to pass them to another Fragment.. Is this the best way to do it? 我想从第二个JSONObject传递值,并将它们添加到HashMap中,然后将其添加到Arraylist中,这样我就可以将它们传递到另一个Fragment。这是最好的方法吗? or How would I best pass this values to another Fragment?
或如何最好地将此值传递给另一个Fragment?
use Fragment.setArguments()
to pass a Bundle instance. 使用
Fragment.setArguments()
传递Bundle实例。 Inside the Bundle you can put a Serializable
obect (your Collection
of HashMap
, for instance) 在Bundle中,您可以放置可
Serializable
对象(例如,您的HashMap
的Collection
)
You have to first create a Bundle, then add the serializable hashmap into the bundle, then set the arguments of the fragment as show below: 您必须首先创建一个Bundle,然后将可序列化的哈希图添加到bundle中,然后设置片段的参数,如下所示:
Bundle args = new Bundle();
args.putSerializable('key', 'value');
Fragment fragment = new Fragment();
fragment.setArguments(args);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.