@@ -32,15 +32,8 @@ of this software and associated documentation files (the "Software"), to deal
3232import java .lang .reflect .Modifier ;
3333import java .math .BigDecimal ;
3434import java .math .BigInteger ;
35- import java .util .Collection ;
36- import java .util .Enumeration ;
37- import java .util .HashMap ;
38- import java .util .Iterator ;
39- import java .util .Locale ;
40- import java .util .Map ;
35+ import java .util .*;
4136import java .util .Map .Entry ;
42- import java .util .ResourceBundle ;
43- import java .util .Set ;
4437
4538/**
4639 * A JSONObject is an unordered collection of name/value pairs. Its external
@@ -1838,4 +1831,31 @@ public Writer write(Writer writer, int indentFactor, int indent)
18381831 throw new JSONException (exception );
18391832 }
18401833 }
1834+
1835+ /**
1836+ * Returns a java.util.Map containing all of the entrys in this object.
1837+ * If an entry in the object is a JSONArray or JSONObject it will also
1838+ * be converted.
1839+ * <p>
1840+ * Warning: This method assumes that the data structure is acyclical.
1841+ *
1842+ * @return a java.util.Map containing the entrys of this object
1843+ */
1844+ public Map <String , Object > toMap () {
1845+ Map <String , Object > results = new HashMap <>();
1846+ for (Entry <String , Object > entry : this .map .entrySet ()) {
1847+ Object value ;
1848+ if (entry .getValue () == null || NULL .equals (entry .getValue ())) {
1849+ value = null ;
1850+ } else if (entry .getValue () instanceof JSONObject ) {
1851+ value = ((JSONObject ) entry .getValue ()).toMap ();
1852+ } else if (entry .getValue () instanceof JSONArray ) {
1853+ value = ((JSONArray ) entry .getValue ()).toList ();
1854+ } else {
1855+ value = entry .getValue ();
1856+ }
1857+ results .put (entry .getKey (), value );
1858+ }
1859+ return results ;
1860+ }
18411861}
0 commit comments