package com.bethecoder.tutorials.json_lib.tests; import net.sf.json.JSONObject; public class First { /** * @param args */ public static void main(String[] args) { JSONObject obj = new JSONObject(); obj.put("name", "Sriram"); obj.put("age", 2); obj.put("hobby", "painting"); System.out.println(obj.toString(2)); //pretty print with indent } }
運行結果java
{ "name": "Sriram", "age": 2, "hobby": "painting" }
package com.bethecoder.tutorials.json_lib.tests; import java.util.Date; import java.util.HashMap; import java.util.Map; import net.sf.json.JSONObject; import net.sf.json.JSONSerializer; public class Map2Json { /** * @param args */ public static void main(String[] args) { Map<String, Object> map = new HashMap<String, Object>(); map.put("name", "Sriram"); map.put("age", 2); map.put("dob", new Date(110, 4, 6)); map.put("hobby", "painting"); JSONObject obj = (JSONObject) JSONSerializer.toJSON(map); System.out.println(obj.toString(2)); //pretty print with indent } }
運行結果:json
{ "dob": { "date": 6, "day": 4, "hours": 0, "minutes": 0, "month": 4, "seconds": 0, "time": 1273084200000, "timezoneOffset": -330, "year": 110 }, "age": 2, "name": "Sriram", "hobby": "painting" }
package com.bethecoder.tutorials.json_lib.tests; import java.util.Arrays; import net.sf.json.JSONObject; public class JsonAddElement { /** * @param args */ public static void main(String[] args) { JSONObject obj = new JSONObject() .element("name", "Sriram") .element("age", 2) .element("hobby", "painting") .element("certificates", Arrays.asList("SCJA", "SCJP", "SCWCD")); System.out.println(obj.toString(2)); //pretty print with indent } }
運行結果:code
{ "name": "Sriram", "age": 2, "hobby": "painting", "certificates": [ "SCJA", "SCJP", "SCWCD" ] }
package com.bethecoder.tutorials.json_lib.tests; import net.sf.json.JSONObject; public class JsonAccumulate { /** * @param args */ public static void main(String[] args) { JSONObject jsonObj = new JSONObject(); jsonObj.accumulate("certificates", "SCJA"); System.out.println(jsonObj.toString(2)); jsonObj.accumulate("certificates", "SCJP"); jsonObj.accumulate("certificates", "SCWCD"); System.out.println(jsonObj.toString(2)); //pretty print with indent } }
運行結果:three
{"certificates": "SCJA"} {"certificates": [ "SCJA", "SCJP", "SCWCD" ]}
package com.bethecoder.tutorials.json_lib.tests; import java.util.HashMap; import java.util.Map; import net.sf.json.JSONObject; public class JsonAccumulateAll { /** * @param args */ public static void main(String[] args) { JSONObject jsonObj = new JSONObject(); jsonObj.put("certificates", "SCJA"); jsonObj.put("prev_ranks", "Rank#2"); System.out.println(jsonObj.toString(2)); Map<String, Object> map = new HashMap<String, Object>(); map.put("certificates", "SCJP"); map.put("prev_ranks", "Rank#1"); jsonObj.accumulateAll(map); System.out.println(jsonObj.toString(2)); //pretty print with indent } }
運行結果:element
{ "certificates": "SCJA", "prev_ranks": "Rank#2" } { "certificates": [ "SCJA", "SCJP" ], "prev_ranks": [ "Rank#2", "Rank#1" ] }
package com.bethecoder.tutorials.json_lib.tests; import net.sf.json.JSONArray; public class JsonArrayCreation { /** * @param args */ public static void main(String[] args) { JSONArray jsonArray = new JSONArray(); jsonArray.add("one"); jsonArray.add(new Integer(2)); jsonArray.add(new Long(3)); jsonArray.add(new Double(4.26)); jsonArray.add(true); jsonArray.add(new char [] { 'A', 'B', 'C' }); System.out.println(jsonArray.toString(2)); //pretty print with indent } }
運行結果:it
[ "one", 2, 3, 4.26, true, [ "A", "B", "C" ] ]
package com.bethecoder.tutorials.json_lib.tests; import net.sf.json.JSONArray; public class JsonArrayAddElement { /** * @param args */ public static void main(String[] args) { JSONArray jsonArray = new JSONArray() .element("one") .element(new Integer(2)) .element(new Long(3)) .element(new Double(4.00)) .element(true) .element(new char [] { 'A', 'B', 'C' }); System.out.println(jsonArray.toString(2)); //pretty print with indent } }
運行結果:io
[ "one", 2, 3, 4, true, [ "A", "B", "C" ] ]
package com.bethecoder.tutorials.json_lib.tests; import net.sf.json.JSONArray; import net.sf.json.JSONSerializer; public class Array2JsonArray { /** * @param args */ public static void main(String[] args) { Object [] genArray = new Object [] { "one", new Integer(2), new Long(3), new Double(4.26), true, new char [] { 'A', 'B', 'C' } }; JSONArray jsonArray = (JSONArray) JSONSerializer.toJSON(genArray); System.out.println(jsonArray.toString(2)); //pretty print with indent } }
運行結果:class
[ "one", 2, 3, 4.26, true, [ "A", "B", "C" ] ]
package com.bethecoder.tutorials.json_lib.tests; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import net.sf.json.JSONArray; import net.sf.json.JSONSerializer; public class Collection2JsonArray { /** * @param args */ public static void main(String[] args) { List<String> strs = Arrays.asList("one", "two", "three", "four"); JSONArray jsonArray = (JSONArray) JSONSerializer.toJSON(strs); System.out.println(jsonArray.toString(2)); //pretty print with indent List<Object> genList = new ArrayList<Object>(); genList.add("one"); genList.add(new Integer(2)); genList.add(new Long(3)); genList.add(new Double(4.26)); genList.add(true); genList.add(new char [] { 'A', 'B', 'C' }); jsonArray = (JSONArray) JSONSerializer.toJSON(genList); System.out.println(jsonArray.toString(2)); //pretty print with indent } }
運行結果:test
[ "one", "two", "three", "four" ] [ "one", 2, 3, 4.26, true, [ "A", "B", "C" ] ]