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:
parent
2657915293
commit
f024b52108
2 changed files with 56 additions and 13 deletions
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue