Here is some code using java 6 to get you started:
JSONObject jo = new JSONObject();
jo.put("firstName", "John");
jo.put("lastName", "Doe");
JSONArray ja = new JSONArray();
ja.put(jo);
JSONObject mainObj = new JSONObject();
mainObj.put("employees", ja);
Edit: Since there has been a lot of confusion about put vs add here I will attempt to explain the difference. In java 6 org.json.JSONArray contains the put method and in java 7 javax.json contains the add method.
An example of this using the builder pattern in java 7 looks something like this:
JsonObject jo = Json.createObjectBuilder()
.add("employees", Json.createArrayBuilder()
.add(Json.createObjectBuilder()
.add("firstName", "John")
.add("lastName", "Doe")))
.build();
Answer from Grammin on Stack OverflowHere is some code using java 6 to get you started:
JSONObject jo = new JSONObject();
jo.put("firstName", "John");
jo.put("lastName", "Doe");
JSONArray ja = new JSONArray();
ja.put(jo);
JSONObject mainObj = new JSONObject();
mainObj.put("employees", ja);
Edit: Since there has been a lot of confusion about put vs add here I will attempt to explain the difference. In java 6 org.json.JSONArray contains the put method and in java 7 javax.json contains the add method.
An example of this using the builder pattern in java 7 looks something like this:
JsonObject jo = Json.createObjectBuilder()
.add("employees", Json.createArrayBuilder()
.add(Json.createObjectBuilder()
.add("firstName", "John")
.add("lastName", "Doe")))
.build();
I suppose you're getting this JSON from a server or a file, and you want to create a JSONArray object out of it.
String strJSON = ""; // your string goes here
JSONArray jArray = (JSONArray) new JSONTokener(strJSON).nextValue();
// once you get the array, you may check items like
JSONOBject jObject = jArray.getJSONObject(0);
Hope this helps :)
java - How to get array from JSON Object? - Stack Overflow
How to turn json objects into an array of json objects using Java?
Jackson parsing JSON containing an array of objects and array of maps w/ dynamic keys
streaming a large json object to a file to avoid memory allocation error
Well, JSON.stringify and JSON.parse are synchronous methods that unfortunately you can't pipe to and from. Like u/nissen2 suggests, you'll find some modules out there that will provide you what you're looking for.
Ah, I think I got it. Is there a way to permanently increase the amount of memory that node has access to? Thx.
While this may solve your problem in the mean time, I wouldn't suggest simply allocating more memory (especially with a huge JSON object.)
More on reddit.comVideos
Hi. I'm new to java and stuck wondering how I would go about turning something like this: {} {} {} into this: [{},{},{}]. I'm trying to figure out how to do this to my incoming json data using Java. The data has like 20 similar objects, with the same keys but with constantly changing values.
Any help is much appreciated.
Sincerely,
your friendly neighborhood noob
The exception occurred because your input JSON string is not properly assigned to JSONObject. There is no constructor like JSONObject(StringBuffer). Just Change it to JSONObject(String) or JSONObject(StringBufferObject.toString())
also, add ]} at the end of your input JSON string.
public static void parseJson(StringBuffer sb) throws JSONException {
JSONObject obj = new JSONObject(sb.toString());
JSONArray arr = obj.getJSONArray("value");
for (int i = 0; i < arr.length(); i++) {
System.out.println(arr.getJSONObject(i).getString("Name"));
}
}
Well, it works for me in my environment. Add " ]} " at the end.
