Skip to content

Instantly share code, notes, and snippets.

@a-v-ebrahimi
Created February 9, 2015 07:46
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save a-v-ebrahimi/283c723b7eae8b42730e to your computer and use it in GitHub Desktop.
Save a-v-ebrahimi/283c723b7eae8b42730e to your computer and use it in GitHub Desktop.
Convert LinkedTreeMap key-value pair into JSON
public static JSONObject convertKeyValueToJSON(LinkedTreeMap<String, Object> ltm) {
JSONObject jo=new JSONObject();
Object[] objs = ltm.entrySet().toArray();
for (int l=0;l<objs.length;l++)
{
Map.Entry o= (Map.Entry) objs[l];
try {
if (o.getValue() instanceof LinkedTreeMap)
jo.put(o.getKey().toString(),convertKeyValueToJSON((LinkedTreeMap<String, Object>) o.getValue()));
else
jo.put(o.getKey().toString(),o.getValue());
} catch (JSONException e) {
e.printStackTrace();
}
}
return jo;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment