JSONObject json = new JSONObject(yourdata);
String statistics = json.getString("statistics");
JSONObject name1 = json.getJSONObject("John");
String ageJohn = name1.getString("Age");
For getting those items in a dynamic way:
JSONObject json = new JSONObject(yourdata);
String statistics = json.getString("statistics");
for (Iterator key=json.keys();key.hasNext();) {
JSONObject name = json.get(key.next());
//now name contains the firstname, and so on...
}
Answer from Emanuel on Stack OverflowOracle
docs.oracle.com › javaee › 7 › api › javax › json › JsonObject.html
JsonObject (Java(TM) EE 7 Specification APIs)
javax.json · All Superinterfaces: JsonStructure, JsonValue, Map<String,JsonValue> public interface JsonObject extends JsonStructure, Map<String,JsonValue> JsonObject class represents an immutable JSON object value (an unordered collection of zero or more name/value pairs).
Videos
14:44
Create Complex Json in Java | Json Object | Json Array In Java ...
12:46
Introduction to JSON and Java JSONObject - YouTube
19:05
Create JSON with Java, Part 2: Json, JsonObject, JsonArray - YouTube
12:32
Reading / Parsing Data from a JSONObject - YouTube
05:47
Create Java classes from JSON Object - YouTube
- YouTube
Top answer 1 of 2
20
JSONObject json = new JSONObject(yourdata);
String statistics = json.getString("statistics");
JSONObject name1 = json.getJSONObject("John");
String ageJohn = name1.getString("Age");
For getting those items in a dynamic way:
JSONObject json = new JSONObject(yourdata);
String statistics = json.getString("statistics");
for (Iterator key=json.keys();key.hasNext();) {
JSONObject name = json.get(key.next());
//now name contains the firstname, and so on...
}
2 of 2
2
You did not specify which library you intend to use to represent the JSON object. Usually there are methods to enumerate the properties of the object. For example:
org.json.JSONObject.keys()
returns an iterator of the String names in the object.
Stleary
stleary.github.io › JSON-java › org › json › JSONObject.html
JSONObject
A JSONObject is an unordered collection of name/value pairs. Its external form is a string wrapped in curly braces with colons between the names and values, and commas between the values and names. The internal form is an object having get and opt methods for accessing the values by name, and ...
TutorialsPoint
tutorialspoint.com › json › json_java_example.htm
JSON with Java
Following is another example that shows a JSON object streaming using Java JSONObject − · import org.json.simple.JSONObject; class JsonEncodeDemo { public static void main(String[] args) { JSONObject obj = new JSONObject(); obj.put("name","foo"); obj.put("num",new Integer(100)); obj.put("balance",new Double(1000.21)); obj.put("is_vip",new Boolean(true)); StringWriter out = new StringWriter(); obj.writeJSONString(out); String jsonText = out.toString(); System.out.print(jsonText); } }
Processing
processing.github.io › processing-javadocs › core › processing › data › JSONObject.html
JSONObject
A JSONObject is an unordered collection of name/value pairs. Its external form is a string wrapped in curly braces with colons between the names and values, and commas between the values and names. The internal form is an object having get and opt methods for accessing the values by name, and ...
Medium
medium.com › @papapapaya11 › exploring-various-ways-to-handle-jsonobject-in-java-6edef1f0561c
Exploring Various Ways to Handle JSONObject in Java | by Little Miss Brave | Medium
December 2, 2024 - String jsonTemplate = "{\"greeting\": \"Hello, ${name}!\", \"age\": ${age}}"; Map<String, String> values = new HashMap<>(); values.put("name", "Little Miss Brave"); values.put("age", "26"); StringSubstitutor substitutor = new StringSubstitutor(values); String result = substitutor.replace(jsonTemplate); JSONObject jsonObject = JSONObject.fromObject(result); System.out.println(jsonObject); // {"greeting": "Hello, Little Miss Brave!", "age": 26} If there are other special use cases, they will be continuously updated here. By mastering these methods, you’ll be well-equipped to handle JSON data efficiently in Java applications.
SourceForge
json-lib.sourceforge.net › apidocs › net › sf › json › JSONObject.html
JSONObject (Overview (json-lib jdk 1.3 API))
A JSONObject is an unordered collection of name/value pairs. Its external form is a string wrapped in curly braces with colons between the names and values, and commas between the values and names. The internal form is an object having get and opt methods for accessing the values by name, and ...
Oracle
docs.oracle.com › cd › F10042_01 › reference-java-api › oracle › adfmf › json › JSONObject.html
JSONObject (Oracle Fusion Middleware Java API Reference for Oracle Mobile Application Framework)
Oracle Fusion Middleware Java API Reference for Oracle Mobile Application Framework 2.5.1.0.0 E92592-01 ... A JSONObject is an unordered collection of name/value pairs. Its external form is a string wrapped in curly braces with colons between the names and values, and commas between the values ...
GitHub
github.com › stleary › JSON-java › blob › master › src › main › java › org › json › JSONObject.java
JSON-java/src/main/java/org/json/JSONObject.java at master · stleary/JSON-java
private static void attemptWriteValue(Writer writer, int indentFactor, int indent, Entry<String, ?> entry, String key) { ... "JSONObject[" + quote(key) + "] is not a " + valueType + " (" + value.getClass() + ")."
Author stleary
Android Developers
developer.android.com › api reference › jsonobject
JSONObject | API reference | Android Developers
Skip to main content · English · Deutsch · Español – América Latina · Français · Indonesia · Polski · Português – Brasil · Tiếng Việt · 中文 – 简体
SourceForge
json-lib.sourceforge.net › apidocs › jdk15 › net › sf › json › JSONObject.html
JSONObject (Overview (json-lib jdk 5 API))
A JSONObject is an unordered collection of name/value pairs. Its external form is a string wrapped in curly braces with colons between the names and values, and commas between the values and names. The internal form is an object having get and opt methods for accessing the values by name, and ...
Oracle
docs.oracle.com › javame › 8.0 › api › json › api › com › oracle › json › JsonObject.html
JsonObject (JSON Documentation)
JsonStructure, JsonValue, java.util.Map<java.lang.String,JsonValue> public interface JsonObject extends JsonStructure, java.util.Map<java.lang.String,JsonValue> JsonObject class represents an immutable JSON object value (an unordered collection of zero or more name/value pairs).
Top answer 1 of 5
158
I'm assuming you want to store the interestKeys in a list.
Using the org.json library:
JSONObject obj = new JSONObject("{interests : [{interestKey:Dogs}, {interestKey:Cats}]}");
List<String> list = new ArrayList<String>();
JSONArray array = obj.getJSONArray("interests");
for(int i = 0 ; i < array.length() ; i++){
list.add(array.getJSONObject(i).getString("interestKey"));
}
2 of 5
13
public class JsonParsing {
public static Properties properties = null;
public static JSONObject jsonObject = null;
static {
properties = new Properties();
}
public static void main(String[] args) {
try {
JSONParser jsonParser = new JSONParser();
File file = new File("src/main/java/read.json");
Object object = jsonParser.parse(new FileReader(file));
jsonObject = (JSONObject) object;
parseJson(jsonObject);
} catch (Exception ex) {
ex.printStackTrace();
}
}
public static void getArray(Object object2) throws ParseException {
JSONArray jsonArr = (JSONArray) object2;
for (int k = 0; k < jsonArr.size(); k++) {
if (jsonArr.get(k) instanceof JSONObject) {
parseJson((JSONObject) jsonArr.get(k));
} else {
System.out.println(jsonArr.get(k));
}
}
}
public static void parseJson(JSONObject jsonObject) throws ParseException {
Set<Object> set = jsonObject.keySet();
Iterator<Object> iterator = set.iterator();
while (iterator.hasNext()) {
Object obj = iterator.next();
if (jsonObject.get(obj) instanceof JSONArray) {
System.out.println(obj.toString());
getArray(jsonObject.get(obj));
} else {
if (jsonObject.get(obj) instanceof JSONObject) {
parseJson((JSONObject) jsonObject.get(obj));
} else {
System.out.println(obj.toString() + "\t"
+ jsonObject.get(obj));
}
}
}
}}
Miamarti
miamarti.github.io › HorusFramework › javadoc › org › json › simple › JSONObject.html
JSONObject
A JSON object. Key value pairs are unordered. JSONObject supports java.util.Map interface.
Top answer 1 of 6
266
org.json.JSONArray may be what you want.
String message;
JSONObject json = new JSONObject();
json.put("name", "student");
JSONArray array = new JSONArray();
JSONObject item = new JSONObject();
item.put("information", "test");
item.put("id", 3);
item.put("name", "course1");
array.put(item);
json.put("course", array);
message = json.toString();
// message
// {"course":[{"id":3,"information":"test","name":"course1"}],"name":"student"}
2 of 6
13
In contrast to what the accepted answer proposes, the documentation says that for JSONArray() you must use put(value) no add(value).
https://developer.android.com/reference/org/json/JSONArray.html#put(java.lang.Object)
(Android API 19-27. Kotlin 1.2.50)
Couchbase
docs.couchbase.com › sdk-api › couchbase-java-client › com › couchbase › client › java › json › JsonObject.html
JsonObject (Couchbase Java SDK 3.11.1 API)
Constructs a JsonObject from a Map<String, ?>. This is only possible if the given Map is well formed, that is it contains non null keys, and all values are of a supported type. A null input Map or null key will lead to a NullPointerException being thrown. If any unsupported value is present in the Map, an InvalidArgumentException will be thrown.