Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. package com.silkimen.http;
  2. import java.util.ArrayList;
  3. import java.util.HashMap;
  4. import java.util.Iterator;
  5. import org.json.JSONArray;
  6. import org.json.JSONException;
  7. import org.json.JSONObject;
  8. public class JsonUtils {
  9. public static HashMap<String, String> getStringMap(JSONObject object) throws JSONException {
  10. HashMap<String, String> map = new HashMap<String, String>();
  11. if (object == null) {
  12. return map;
  13. }
  14. Iterator<?> i = object.keys();
  15. while (i.hasNext()) {
  16. String key = (String) i.next();
  17. map.put(key, object.getString(key));
  18. }
  19. return map;
  20. }
  21. public static HashMap<String, Object> getObjectMap(JSONObject object) throws JSONException {
  22. HashMap<String, Object> map = new HashMap<String, Object>();
  23. if (object == null) {
  24. return map;
  25. }
  26. Iterator<?> i = object.keys();
  27. while (i.hasNext()) {
  28. String key = (String) i.next();
  29. Object value = object.get(key);
  30. if (value instanceof JSONArray) {
  31. map.put(key, getObjectList((JSONArray) value));
  32. } else {
  33. map.put(key, object.get(key));
  34. }
  35. }
  36. return map;
  37. }
  38. public static ArrayList<Object> getObjectList(JSONArray array) throws JSONException {
  39. ArrayList<Object> list = new ArrayList<Object>();
  40. for (int i = 0; i < array.length(); i++) {
  41. list.add(array.get(i));
  42. }
  43. return list;
  44. }
  45. }