Transform a Map to a JSON object

1 vote · 0 comments

JSON is a convenient format for transporting info across the wire.

raw ·
copy
· download
public static JSONObject getJSON(Map map) { Iterator iter = map.entrySet().iterator(); JSONObject holder = new JSONObject(); while (iter.hasNext()) { Map.Entry pairs = (Map.Entry) iter.next(); String key = (String) pairs.getKey(); Map m = (Map) pairs.getValue(); JSONObject data = new JSONObject(); try { Iterator iter2 = m.entrySet().iterator(); while (iter2.hasNext()) { Map.Entry pairs2 = (Map.Entry) iter2.next(); data.put((String) pairs2.getKey(), (String) pairs2.getValue()); } holder.put(key, data); } catch (JSONException e) { Log.e("Transforming", "There was an error packaging JSON",e); } } return holder; }

Be the first to comment

Sign in with OpenID