1
0
Fork 0
mirror of https://github.com/ethauvin/JSON-java.git synced 2025-06-17 07:50:52 -07:00

Adds JSONArray toList method and JSONObject toMap method

This commit is contained in:
Joe Ferner 2016-02-26 23:46:41 -05:00
parent 2657915293
commit f024b52108
2 changed files with 56 additions and 13 deletions

View file

@ -32,15 +32,8 @@ import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Collection;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Locale;
import java.util.Map;
import java.util.*;
import java.util.Map.Entry;
import java.util.ResourceBundle;
import java.util.Set;
/**
* A JSONObject is an unordered collection of name/value pairs. Its external
@ -1838,4 +1831,31 @@ public class JSONObject {
throw new JSONException(exception);
}
}
/**
* Returns a java.util.Map containing all of the entrys in this object.
* If an entry in the object is a JSONArray or JSONObject it will also
* be converted.
* <p>
* Warning: This method assumes that the data structure is acyclical.
*
* @return a java.util.Map containing the entrys of this object
*/
public Map<String, Object> toMap() {
Map<String, Object> results = new HashMap<>();
for (Entry<String, Object> entry : this.map.entrySet()) {
Object value;
if (entry.getValue() == null || NULL.equals(entry.getValue())) {
value = null;
} else if (entry.getValue() instanceof JSONObject) {
value = ((JSONObject) entry.getValue()).toMap();
} else if (entry.getValue() instanceof JSONArray) {
value = ((JSONArray) entry.getValue()).toList();
} else {
value = entry.getValue();
}
results.put(entry.getKey(), value);
}
return results;
}
}