EncodingExamples
Examples of JSON encoding
Example 1-1 - Encode a JSON object//import org.json.simple.JSONObject; 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)); obj.put("nickname",null); System.out.print(obj); Result: {"balance":1000.21,"num":100,"nickname":null,"is_vip":true,"name":"foo"} JSONObject is subclass of java.util.HashMap. No ordering is provided. If you need strict ordering of elements use JSONValue.toJSONString( map ) method with ordered map implementation such as java.util.LinkedHashMap (see example 1-3).Please refer Mapping Between JSON and Java Entities for more information. Example 1-2 - Encode a JSON object - Streaming//import org.json.simple.JSONObject; 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)); obj.put("nickname",null); StringWriter out = new StringWriter(); obj.writeJSONString(out); String jsonText = out.toString(); System.out.print(jsonText); Result: {"balance":1000.21,"num":100,"nickname":null,"is_vip":true,"name":"foo"} JSONObject is subclass of java.util.HashMap. No ordering is provided. If you need strict ordering of elements use JSONValue.toJSONString( map ) method with ordered map implementation such as java.util.LinkedHashMap (see example 1-3). Please refer Mapping Between JSON and Java Entities for more information. Example 1-3 - Encode a JSON object - Using Map//import java.util.LinkedHashMap; //import java.util.Map; //import org.json.simple.JSONValue; Map obj=new LinkedHashMap(); obj.put("name","foo"); obj.put("num",new Integer(100)); obj.put("balance",new Double(1000.21)); obj.put("is_vip",new Boolean(true)); obj.put("nickname",null); String jsonText = JSONValue.toJSONString(obj); System.out.print(jsonText); Result: {"name":"foo","num":100,"balance":1000.21,"is_vip":true,"nickname":null} Now the order of the object entries is preserved, which is different from example 1-1. Please refer Mapping Between JSON and Java Entities for more information. Example 1-4 - Encode a JSON object - Using Map and streaming//import java.util.LinkedHashMap; //import java.util.Map; //import org.json.simple.JSONValue; Map obj=new LinkedHashMap(); obj.put("name","foo"); obj.put("num",new Integer(100)); obj.put("balance",new Double(1000.21)); obj.put("is_vip",new Boolean(true)); obj.put("nickname",null); StringWriter out = new StringWriter(); JSONValue.writeJSONString(obj, out); String jsonText = out.toString(); System.out.print(jsonText); Result: {"name":"foo","num":100,"balance":1000.21,"is_vip":true,"nickname":null} Please refer Mapping Between JSON and Java Entities for more information. Example 2-1 - Encode a JSON array//import org.json.simple.JSONArray; JSONArray list = new JSONArray(); list.add("foo"); list.add(new Integer(100)); list.add(new Double(1000.21)); list.add(new Boolean(true)); list.add(null); System.out.print(list); Result: ["foo",100,1000.21,true,null] Example 2-2 - Encode a JSON array - Streaming//import org.json.simple.JSONArray; JSONArray list = new JSONArray(); list.add("foo"); list.add(new Integer(100)); list.add(new Double(1000.21)); list.add(new Boolean(true)); list.add(null); StringWriter out = new StringWriter(); list.writeJSONString(out); String jsonText = out.toString(); System.out.print(jsonText); Result: ["foo",100,1000.21,true,null] Please refer Mapping Between JSON and Java Entities for more information. Example 2-3 - Encode a JSON array - Using List//import org.json.simple.JSONValue; LinkedList list = new LinkedList(); list.add("foo"); list.add(new Integer(100)); list.add(new Double(1000.21)); list.add(new Boolean(true)); list.add(null); String jsonText = JSONValue.toJSONString(list); System.out.print(jsonText); Result: ["foo",100,1000.21,true,null] Please refer Mapping Between JSON and Java Entities for more information. Example 2-4 - Encode a JSON array - Using List and streaming//import org.json.simple.JSONValue; LinkedList list = new LinkedList(); list.add("foo"); list.add(new Integer(100)); list.add(new Double(1000.21)); list.add(new Boolean(true)); list.add(null); StringWriter out = new StringWriter(); JSONValue.writeJSONString(list, out); String jsonText = out.toString(); System.out.print(jsonText); Result: ["foo",100,1000.21,true,null] Please refer Mapping Between JSON and Java Entities for more information. Example 3 - Merge two JSON objects//import org.json.simple.JSONObject; JSONObject obj1 = new JSONObject(); obj1.put("name","foo"); obj1.put("num",new Integer(100)); obj1.put("balance",new Double(1000.21)); JSONObject obj2 = new JSONObject(); obj2.put("is_vip",new Boolean(true)); obj2.put("nickname",null); obj2.putAll(obj1); System.out.print(obj2); Result: {"balance":1000.21,"num":100,"nickname":null,"is_vip":true,"name":"foo"}, the same as the one of Example 1. Example 4 - Merge two JSON arraysJSONArray list1 = new JSONArray(); list1.add("foo"); list1.add(new Integer(100)); list1.add(new Double(1000.21)); JSONArray list2 = new JSONArray(); list2.add(new Boolean(true)); list2.add(null); list2.addAll(list1); System.out.print(list2); Result: [true,null,"foo",100,1000.21], the order of which is different from the one of Example 2. Example 5-1 - Combination of JSON primitives, JSON object and JSON arraysJSONArray list1 = new JSONArray(); list1.add("foo"); list1.add(new Integer(100)); list1.add(new Double(1000.21)); JSONArray list2 = new JSONArray(); list2.add(new Boolean(true)); list2.add(null); 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)); obj.put("nickname",null); obj.put("list1", list1); obj.put("list2", list2); System.out.println(obj); Result: {"balance":1000.21,"list2":[true,null],"num":100,"list1":["foo",100,1000.21],"nickname":null,"is_vip":true,"name":"foo"} Example 5-2 - Combination of JSON primitives, Map and ListMap m1 = new LinkedHashMap(); Map m2 = new HashMap(); List l1 = new LinkedList(); m1.put("k11","v11"); m1.put("k12","v12"); m1.put("k13", "v13"); m2.put("k21","v21"); m2.put("k22","v22"); m2.put("k23","v23"); l1.add(m1); l1.add(m2); String jsonString = JSONValue.toJSONString(l1); System.out.println(jsonString); Result: [{"k11":"v11","k12":"v12","k13":"v13"},{"k22":"v22","k21":"v21","k23":"v23"}] Example 5-3 - Combination of JSON primitives, JSONObject, Map and List, and streamingStringWriter out = new StringWriter(); JSONObject obj = new JSONObject(); LinkedHashMap m1 = new LinkedHashMap(); LinkedList l1 = new LinkedList(); obj.put("k1", "v1"); obj.put("k2", m1); obj.put("k3", l1); m1.put("mk1", "mv1"); l1.add("lv1"); l1.add("lv2"); m1.put("mk2", l1); obj.writeJSONString(out); System.out.println("jsonString:"); System.out.println(out.toString()); String jsonString = obj.toJSONString(); System.out.println(jsonString); Result:jsonString: {"k3":["lv1","lv2"],"k1":"v1","k2":{"mk1":"mv1","mk2":["lv1","lv2"]}} {"k3":["lv1","lv2"],"k1":"v1","k2":{"mk1":"mv1","mk2":["lv1","lv2"]}} Example 6-1 - Customize JSON outputs/*class User implements JSONAware{ private int id; private String name; private String password; public User(int id, String name, String password){ this.id = id; this.name = name; this.password = password; } public String toJSONString(){ StringBuffer sb = new StringBuffer(); sb.append("{"); sb.append(JSONObject.escape("userName")); sb.append(":"); sb.append("\"" + JSONObject.escape(name) + "\""); sb.append(","); sb.append(JSONObject.escape("ID")); sb.append(":"); sb.append(id); sb.append("}"); return sb.toString(); } }*/ JSONArray users = new JSONArray(); users.add(new User(123,"foo1", "secret1")); users.add(new User(124,"foo2", "secret2")); users.add(new User(125,"\"foo2\"", "secret2")); System.out.println(users); Result: [{userName:"foo1",ID:123},{userName:"foo2",ID:124},{userName:"\"foo2\"",ID:125}] User.toJSONString() seems to be a bit complicated. The purpose is to demonstrate the usage of JSONObject.escape(). It can be simpler:public String toJSONString(){ JSONObject obj = new JSONObject(); obj.put("userName", name); obj.put("ID", new Integer(id)); return obj.toString(); }Please refer Mapping Between JSON and Java Entities for more information. (Note: If you are using version 1.0.2 or earlier, you need to override Object.toString() of your bean to get customized output.) Example 6-2 - Customize JSON outputs - Streaming/*class User implements JSONStreamAware{ private int id; private String name; private String password; public User(int id, String name, String password){ this.id = id; this.name = name; this.password = password; } public void writeJSONString (Writer out) throws IOException{ LinkedHashMap obj = new LinkedHashMap(); obj.put("userName", name); obj.put("ID", new Integer(id)); JSONValue.writeJSONString(obj, out); } }*/ JSONArray users = new JSONArray(); users.add(new User(123,"foo1", "secret1")); users.add(new User(124,"foo2", "secret2")); users.add(new User(125,"\"foo2\"", "secret2")); StringWriter out = new StringWriter(); users.writeJSONString(out); System.out.println(out.toString()); Result: [{"userName":"foo1","ID":123},{"userName":"foo2","ID":124},{"userName":"\"foo2\"","ID":125}] Please note that you don't have to implement JSONStreamAware to support streaming output of your bean, you can only implement JSONAware instead of JSONStreamAware. In the latter case, JSONAware.toString() is called and the result is written to the output stream. You can implement JSONStreamAware for better performance. If a class implements both JSONStreamAware and JSONAware, JSONStreamAware is given precedence while streaming. Please refer Mapping Between JSON and Java Entities for more information. |
Examples of JSON encoding
最新推荐文章于 2020-12-09 14:32:42 发布
i have a very complex data structure with me its LinkedHashmap? of LinkedHashmap? of LinkedHashmap? of ArrayList?. when i try to use JSONValue.toJSONString it does not work properly. can you suggest what should i do?
my main aim is to maintain the order of the objects in my collection i was using net.sf.json.JSONObject till now. but its messing up with the order in my linkedHashmaps.
can you suggest what should i do?
JSONValue.toJSONString() should preserve the order of entries determined by the LinkedHashMap?. Could you provide sample code on the discussion group? Thanks.
Here's the example: http://groups.google.com/group/json-simple/browse_thread/thread/be4488d4db3e1bb9
This is great, thank you :)
Hi,
I need that the output must not be escaped. Unfortunately, I didn't see an easy way to do that (unable to derive JSONObject because there are static methods. So I'll write a simple method like this called when the process is done:
Maybe, could you create a new constructor that prevent to escape the output...
But not escaping output is really dangerous! If you by accident encode a string containing quotes, newlines or similar, it will break. If at all possible, challenge this requirement.
Anyway, you could write your code simpler and probably more efficient as protected String removeEscapeChar(final String aString) {
} or use StringUtils ? form apache commons lang: StringUtils ?.remove(aString, '\\');At the very least, you should change if (c != 92) to if (c != '\\') - using the ascii code is just obscure.
how to encode json coming from a link. ex: http://google.com/json_folder
Handling multiple Object Convert multiple Object to Key pair String
//sample/Student.java
package sample; public class Student {
}
//Example.jsp
<%@page import="org.json.simple.JSONValue"%> <%@page import="java.util.Iterator"%> <%@page import="java.util.ArrayList?"%> <%@page import="sample."%> <%@page import="org.json.simple.JSONObject"%>
output : {" JString" :{"1":{"rollno":1,"marks":60,"name":"Chidam"},"2":{"rollno":2,"marks":90,"name":"Sundar"}}}
thanks for a really good package!
I have a structure like this ,
[Note{"id:""1","title:""Abcd","tasks:""[{"id:""1","tascdesc:""sfsfsf"}, {"id:""2","tascdesc:""sfsfsf"}]" }] Arraylist of Note object each Note has Arraylist of task object
I am unable to encode using Json Simple. Any help will be appreciated .Thank you!
(assumed intended object structure)
output: [{"Note":{"id":"1","title":"Abcd","tasks":[{"id":"1","tascdesc":"desc for tobj1"},{"id":"2","tascdesc":"desc fot tobj2"}]}}]
good for me.
Thanks all!
Can someone please tell me what the difference is between json.simple and org.json?
as it named this is simple and wrappered more Functions for json。other way org.json is the standar.such as JDK OF SUN. and simple josn as the JDK OF IBM.
How to post json request with the help of htmlunit
How to encode json string in struts2 jquery grid? what is jsonmap? please tell me about those two
Excellent Tutorial. Very usefull for me.
Thanks for the useful library!
Can I "prettify" the output? I would like to preserve a decent formatting with large object : i.e. no single line please ;
It does not seem possible to prettify with json-simple. I am using an external library to do such
=> https://code.google.com/p/google-gson/
Gson gson = new GsonBuilder?().setPrettyPrinting().create(); String jsonOutput = gson.toJson(someObject);
that is cool and sound something different
Super bueno la explicacion
What's the best practice for converting a Map where the keys aren't a primitive type but do implement JSONAware?
ex: HashMap?<CustomClass1?, CustomClass2?> myCustomMap;
Right now, JSONObject class .toString() on the key, and .toJSONString() on the entry.
Should I instead, for conversions sake, convert the custom map into an array of keys (stored as a JSONObject with the array index as the key and the original key as the entry) and store the entries using the same set of array indices? Obviously tedious, and hard to manage changes, but I mainly need a way to store and retrieve.
Thoughts?
Is there a way to preserve trailing zeroes in floats?
can anyone help me for attaching more than one file to JSON object in java dynamically? Is there any way for this?