Last Published: 2024-09-23
|
Version: 2.4-jenkins-8-SNAPSHOT
Json.org
|
Maven 2
Json-lib
Introduction
Download
Feature List
Getting Started
Advanced features
Snippets
Groovy integration
Who is using it?
FAQ
Changes
Javadoc (jdk13)
Javadoc (jdk15)
Project Documentation
Project Information
Project Reports
Snippets
From Java to JSON
Creating a JSONObject from scratch
Creating a JSONObject from a JSON formatted string
Creating a JSONObject from a Map
Creating a JSONObject from a JavaBean
Creating a JSONArray from scratch
Creating a JSONArray from a JSON formatted string
Creating a JSONArray from a Collection
Creating a JSONArray from an array
JavaScript functions
Exclude properties
Exclude properties (with filters)
From JSON to Java
JSONObject to DynaBean
JSONObject to JavaBean
JSONArray to List
JSONArray to array
JSONArray to array (type conversion)
JSONObject to JavaBean, exclude properties with filters
From JSON to XML
JSONObject to XML
JSONObject to XML (no type hints)
JSONObject to XML (with json prefix)
JSONObject to XML (change node names)
JSONArray to XML
JSONArray to XML (no type hints)
JSONArray to XML (with json prefix)
Flatten JSONArray into parent
From XML to JSON
XML to JSONObject
Groovy Support
Using the JsonGroovyBuilder
JRuby Support
Creating a JSONObject
Append elements to JSONObject with << (array)
Append elements to JSONObject with << (hash)
From Java to JSON
Creating a JSONObject from scratch
JSONObject jsonObject = new JSONObject() .element( "string", "JSON" ) .element( "integer", "1" ) .element( "double", "2.0" ) .element( "boolean", "true" ); assertEquals( "JSON", jsonObject.getString("string") ); assertEquals( 1, jsonObject.getInt("integer") ); assertEquals( 2.0d, jsonObject.getDouble("double"), 0d ); assertTrue( jsonObject.getBoolean("boolean") );
[
Index
|
From Java to JSON
]
Creating a JSONObject from a JSON formatted string
String str = "{'string':'JSON', 'integer': 1, 'double': 2.0, 'boolean': true}"; JSONObject jsonObject = (JSONObject) JSONSerializer.toJSON( str ); assertEquals( "JSON", jsonObject.getString("string") ); assertEquals( 1, jsonObject.getInt("integer") ); assertEquals( 2.0d, jsonObject.getDouble("double"), 0d ); assertTrue( jsonObject.getBoolean("boolean") );
[
Index
|
From Java to JSON
]
Creating a JSONObject from a Map
Map map = new HashMap(); map.put( "string", "JSON" ); map.put( "integer", "1" ); map.put( "double", "2.0" ); map.put( "boolean", "true" ); JSONObject jsonObject = (JSONObject) JSONSerializer.toJSON( map ); assertEquals( "JSON", jsonObject.getString("string") ); assertEquals( 1, jsonObject.getInt("integer") ); assertEquals( 2.0d, jsonObject.getDouble("double"), 0d ); assertTrue( jsonObject.getBoolean("boolean") );
[
Index
|
From Java to JSON
]
Creating a JSONObject from a JavaBean
public class MyJavaBean { private String string; private int integer; private double dooble; private boolean bool; // getters & setters } MyJavaBean bean = new MyJavaBean(); bean.setString( "JSON" ); bean.setInteger( 1 ); bean.setDooble( 2.0d ); bean.setBool( true ); JSONObject jsonObject = (JSONObject) JSONSerializer.toJSON( bean ); assertEquals( "JSON", jsonObject.getString("string") ); assertEquals( 1, jsonObject.getInt("integer") ); assertEquals( 2.0d, jsonObject.getDouble("dooble"), 0d ); assertTrue( jsonObject.getBoolean("bool") );
[
Index
|
From Java to JSON
]
Creating a JSONArray from scratch
JSONArray jsonArray = new JSONArray() .element( "JSON" ) .element( "1" ) .element( "2.0" ) .element( "true" ); assertEquals( "JSON", jsonArray.getString(0) ); assertEquals( 1, jsonArray.getInt(1) ); assertEquals( 2.0d, jsonArray.getDouble(2), 0d ); assertTrue( jsonArray.getBoolean(3) );
[
Index
|
From Java to JSON
]
Creating a JSONArray from a JSON formatted string
String str = "['JSON', 1, 2.0, true]"; JSONArray jsonArray = (JSONArray) JSONSerializer.toJSON( str ); assertEquals( "JSON", jsonArray.getString(0) ); assertEquals( 1, jsonArray.getInt(1) ); assertEquals( 2.0d, jsonArray.getDouble(2), 0d ); assertTrue( jsonArray.getBoolean(3) );
[
Index
|
From Java to JSON
]
Creating a JSONArray from a Collection
List list = new ArrayList(); list.add( "JSON" ); list.add( "1" ); list.add( "2.0" ); list.add( "true" ); JSONArray jsonArray = (JSONArray) JSONSerializer.toJSON( list ); assertEquals( "JSON", jsonArray.getString(0) ); assertEquals( 1, jsonArray.getInt(1) ); assertEquals( 2.0d, jsonArray.getDouble(2), 0d ); assertTrue( jsonArray.getBoolean(3) );
[
Index
|
From Java to JSON
]
Creating a JSONArray from an array
Object[] array = new Object[]{ "JSON", "1", "2.0", "true" }; JSONArray jsonArray = (JSONArray) JSONSerializer.toJSON( array ); assertEquals( "JSON", jsonArray.getString(0) ); assertEquals( 1, jsonArray.getInt(1) ); assertEquals( 2.0d, jsonArray.getDouble(2), 0d ); assertTrue( jsonArray.getBoolean(3) );
[
Index
|
From Java to JSON
]
JavaScript functions
String str = "{'func': function( param ){ doSomethingWithParam(param); }}"; JSONObject jsonObject = (JSONObject) JSONSerializer.toJSON( str ); JSONFunction func = (JSONFunction) jsonObject.get("func"); ArrayAssertions.assertEquals( new String[]{"param"}, func.getParams() ); assertEquals( "doSomethingWithParam(param);", func.getText() );
[
Index
|
From Java to JSON
]
Exclude properties
String str = "{'string':'JSON', 'integer': 1, 'double': 2.0, 'boolean': true}"; JsonConfig jsonConfig = new JsonConfig(); jsonConfig.setExcludes( new String[]{ "double", "boolean" } ); JSONObject jsonObject = (JSONObject) JSONSerializer.toJSON( str, jsonConfig ); assertEquals( "JSON", jsonObject.getString("string") ); assertEquals( 1, jsonObject.getInt("integer") ); assertFalse( jsonObject.has("double") ); assertFalse( jsonObject.has("boolean") );
[
Index
|
From Java to JSON
]
Exclude properties (with filters)
String str = "{'string':'JSON', 'integer': 1, 'double': 2.0, 'boolean': true}"; JsonConfig jsonConfig = new JsonConfig(); jsonConfig.setJsonPropertyFilter( new PropertyFilter(){ public boolean apply( Object source, String name, Object value ) { if( "double".equals(value) || "boolean".equals(value) ){ return true; } return false; } }); JSONObject jsonObject = (JSONObject) JSONSerializer.toJSON( str, jsonConfig ); assertEquals( "JSON", jsonObject.getString("string") ); assertEquals( 1, jsonObject.getInt("integer") ); assertFalse( jsonObject.has("double") ); assertFalse( jsonObject.has("boolean") );
[
Index
|
From Java to JSON
]
From JSON to Java
JSONObject to DynaBean
String str = "{'string':'JSON', 'integer': 1, 'double': 2.0, 'boolean': true}"; JSONObject jsonObject = (JSONObject) JSONSerializer.toJSON( str ); DynaBean bean = (DynaBean) JSONSerializer.toJava( jsonObject ); assertEquals( "JSON", bean.get("string") ); assertEquals( new Integer(1), bean.get("integer") ); assertEquals( new Double(2.0), bean.get("double") ); assertEquals( Boolean.TRUE, bean.get("boolean") );
[
Index
|
From JSON to Java
]
JSONObject to JavaBean
public class MyJavaBean { private String string; private int integer; private double dooble; private boolean bool; // getters & setters } MyJavaBean bean = new MyJavaBean(); bean.setString( "JSON" ); bean.setInteger( 1 ); bean.setDooble( 2.0d ); bean.setBool( true ); JSONObject jsonObject = (JSONObject) JSONSerializer.toJSON( bean ); JsonConfig jsonConfig = new JsonConfig(); jsonConfig.setRootClass( MyJavaBean.class ); MyJavaBean bean2 = (MyJavaBean) JSONSerializer.toJava( jsonObject, jsonConfig ); assertNotNull( bean2 ); assertEquals( "JSON", bean2.getString() ); assertEquals( 1, bean2.getInteger() ); assertEquals( 2.0d, bean2.getDooble(), 0d ); assertTrue( bean2.getBool() );
[
Index
|
From JSON to Java
]
JSONArray to List
List input = new ArrayList(); input.add( "JSON" ); input.add( "1" ); input.add( "2.0" ); input.add( "true" ); JSONArray jsonArray = (JSONArray) JSONSerializer.toJSON( input ); List output = (List) JSONSerializer.toJava( jsonArray ); ArrayAssertions.assertEquals( input, output );
[
Index
|
From JSON to Java
]
JSONArray to array
List input = new ArrayList(); input.add( "JSON" ); input.add( "1" ); input.add( "2.0" ); input.add( "true" ); JSONArray jsonArray = (JSONArray) JSONSerializer.toJSON( input ); JsonConfig jsonConfig = new JsonConfig(); jsonConfig.setArrayMode( JsonConfig.MODE_OBJECT_ARRAY ); Object[] output = (Object[]) JSONSerializer.toJava( jsonArray, jsonConfig ); Object[] expected = new Object[]{ "JSON", "1", "2.0", "true" }; ArrayAssertions.assertEquals( expected, output);
[
Index
|
From JSON to Java
]
JSONArray to array (type conversion)
List input = new ArrayList(); input.add( "1" ); input.add( "2" ); input.add( "3.0" ); JSONArray jsonArray = (JSONArray) JSONSerializer.toJSON( input ); JsonConfig jsonConfig = new JsonConfig(); jsonConfig.setArrayMode( JsonConfig.MODE_OBJECT_ARRAY ); jsonConfig.setRootClass( Integer.TYPE ); int[] output = (int[]) JSONSerializer.toJava( jsonArray, jsonConfig ); int[] expected = new int[]{ 1, 2, 3 }; ArrayAssertions.assertEquals( expected, output);
[
Index
|
From JSON to Java
]
JSONObject to JavaBean, exclude properties with filters
public class MyJavaBean { private String string; private int integer; private double dooble = 0d; private boolean bool; // getters & setters } MyJavaBean bean = new MyJavaBean(); bean.setString( "JSON" ); bean.setInteger( 1 ); bean.setDooble( 2.0d ); bean.setBool( true ); JSONObject jsonObject = (JSONObject) JSONSerializer.toJSON( bean ); JsonConfig jsonConfig = new JsonConfig(); jsonConfig.setRootClass( MyJavaBean.class ); jsonConfig.setJavaPropertyFilter( new PropertyFilter(){ public boolean apply( Object source, String name, Object value ) { if( "bool".equals( name ) || "dooble".equals( name ) ){ return true; } return false; } }); MyJavaBean bean2 = (MyJavaBean) JSONSerializer.toJava( jsonObject, jsonConfig ); assertNotNull( bean2 ); assertEquals( "JSON", bean2.getString() ); assertEquals( 1, bean2.getInteger() ); assertEquals( 0d, bean2.getDooble(), 0d ); assertFalse( bean2.getBool() );
[
Index
|
From JSON to Java
]
From JSON to XML
JSONObject to XML
String str = "{'name':'JSON','integer':1,'double':2.0,'boolean':true,'nested':{'id':42},'array':[1,2,3]}"; JSON json = JSONSerializer.toJSON( str ); XMLSerializer xmlSerializer = new XMLSerializer(); String xml = xmlSerializer.write( json ); System.out.println(xml);
<?xml version="1.0" encoding="UTF-8"?> <o> <boolean type="boolean">true</boolean> <double type="number">2.0</double> <integer type="number">1</integer> <name type="string">JSON</name> <nested class="object"> <id type="number">42</id> </nested> <array class="array"> <e type="number">1</e> <e type="number">2</e> <e type="number">3</e> </array> </o>
[
Index
|
From JSON to XML
]
JSONObject to XML (no type hints)
String str = "{'name':'JSON','integer':1,'double':2.0,'boolean':true,'nested':{'id':42},'array':[1,2,3]}"; JSON json = JSONSerializer.toJSON( str ); XMLSerializer xmlSerializer = new XMLSerializer(); xmlSerializer.setTypeHintsEnabled( false ); String xml = xmlSerializer.write( json ); System.out.println(xml);
<?xml version="1.0" encoding="UTF-8"?> <o> <boolean>true</boolean> <double>2.0</double> <integer>1</integer> <name>JSON</name> <nested> <id>42</id> </nested> <array> <e>1</e> <e>2</e> <e>3</e> </nested> </o>
[
Index
|
From JSON to XML
]
JSONObject to XML (with json prefix)
String str = "{'name':'JSON','integer':1,'double':2.0,'boolean':true,'nested':{'id':42},'array':[1,2,3]}"; JSON json = JSONSerializer.toJSON( str ); XMLSerializer xmlSerializer = new XMLSerializer(); xmlSerializer.setTypeHintsCompatibility( false ); String xml = xmlSerializer.write( json ); System.out.println(xml);
<?xml version="1.0" encoding="UTF-8"?> <o> <boolean json_type="boolean">true</boolean> <double json_type="number">2.0</double> <integer json_type="number">1</integer> <name json_type="string">JSON</name> <nested json_class="object"> <id json_type="number">42</id> </nested> <array json_class="array"> <e json_type="number">1</e> <e json_type="number">2</e> <e json_type="number">3</e> </array> </o>
[
Index
|
From JSON to XML
]
JSONObject to XML (change node names)
String str = "{'name':'JSON','integer':1,'double':2.0,'boolean':true,'nested':{'id':42},'array':[1,2,3]}"; JSON json = JSONSerializer.toJSON( str ); XMLSerializer xmlSerializer = new XMLSerializer(); xmlSerializer.setRootName( "root" ); xmlSerializer.setElementName( "element" ); String xml = xmlSerializer.write( json ); System.out.println(xml);
<?xml version="1.0" encoding="UTF-8"?> <root> <boolean type="boolean">true</boolean> <double type="number">2.0</double> <integer type="number">1</integer> <name type="string">JSON</name> <nested class="object"> <id type="number">42</id> </nested> <array class="array"> <element type="number">1</e> <element type="number">2</e> <element type="number">3</e> </array> </root>
[
Index
|
From JSON to XML
]
JSONArray to XML
String str = "['JSON', 1, 2.0, true, {'id':42}, [1,2,3]]"; JSON json = JSONSerializer.toJSON( str ); XMLSerializer xmlSerializer = new XMLSerializer(); String xml = xmlSerializer.write( json ); System.out.println(xml);
<?xml version="1.0" encoding="UTF-8"?> <a> <e type="string">JSON</e> <e type="number">1</e> <e type="number">2.0</e> <e type="boolean">true</e> <e class="object"> <id type="number">42</id> </e> <e class="array"> <e type="number">1</e> <e type="number">2</e> <e type="number">3</e> </e> </a>
[
Index
|
From JSON to XML
]
JSONArray to XML (no type hints)
String str = "['JSON', 1, 2.0, true, {'id':42}, [1,2,3]]"; JSON json = JSONSerializer.toJSON( str ); XMLSerializer xmlSerializer = new XMLSerializer(); xmlSerializer.setTypeHintsEnabled( false ); String xml = xmlSerializer.write( json ); System.out.println(xml);
<?xml version="1.0" encoding="UTF-8"?> <a> <e>JSON</e> <e>1</e> <e>2.0</e> <e>true</e> <e> <id>42</id> </e> <e> <e>1</e> <e>2</e> <e>3</e> </e> </a>
[
Index
|
From JSON to XML
]
JSONArray to XML (with json prefix)
String str = "['JSON', 1, 2.0, true, {'id':42}, [1,2,3]]"; JSON json = JSONSerializer.toJSON( str ); XMLSerializer xmlSerializer = new XMLSerializer(); xmlSerializer.setTypeHintsCompatibility( false ); String xml = xmlSerializer.write( json ); System.out.println(xml);
<?xml version="1.0" encoding="UTF-8"?> <a> <e json_type="string">JSON</e> <e json_type="number">1</e> <e json_type="number">2.0</e> <e json_type="boolean">true</e> <e json_class="object"> <id json_type="number">42</id> </e> <e json_class="array"> <e json_type="number">1</e> <e json_type="number">2</e> <e json_type="number">3</e> </e> </a>
[
Index
|
From JSON to XML
]
Flatten JSONArray into parent
String str = "{'number': [1,2,3]}"; JSONObject json = (JSONObject) JSONSerializer.toJSON( str ); XMLSerializer xmlSerializer = new XMLSerializer(); xmlSerializer.setTypeHintsEnabled( false ); String xml = xmlSerializer.write( json ); System.out.println( xml ); json.getJSONArray( "number" ).setExpandElements( true ); xml = xmlSerializer.write( json ); System.out.println( xml );
<?xml version="1.0" encoding="UTF-8"?> <o> <number> <e>1</e> <e>2</e> <e>3</e> </number> </o> <?xml version="1.0" encoding="UTF-8"?> <o> <number>1</number> <number>2</number> <number>3</number> </o>
[
Index
|
From JSON to XML
]
From XML to JSON
XML to JSONObject
<o number="1"> first <string>json</string> <array> <e>1</e> <e>true</e> </array> </o>
String xml = ... XMLSerializer xmlSerializer = new XMLSerializer(); JSON json = xmlSerializer.read( xml ); System.out.println( json.toString(2) );
{ "string": "json", "#text": "first", "array": [ "1", "true" ], "@number": "1" }
[
Index
|
From XML to JSON
]
Groovy Support
Using the JsonGroovyBuilder
The following snippets produce the same JSON output
def builder = new JsonGroovyBuilder() def books = builder.books { book = [title: "Groovy in Action", author: "Dierk Konig"] book = [title: "Groovy in Action", author: "Dierk Konig"] }
def builder = new JsonGroovyBuilder() def books = builder.books { book = new Book(title: "Groovy in Action", author: "Dierk Konig") }
def builder = new JsonGroovyBuilder() def books = builder.books { book = { title = "Groovy in Action" author= "Dierk Konig" } book = { title = "Groovy in Action" author= "Dierk Konig" } }
def builder = new JsonGroovyBuilder() def books = builder.books { 2.times { book = { title = "Groovy in Action" author= "Dierk Konig" } } }
def builder = new JsonGroovyBuilder() def books = builder.books { 2.times { book { title = "Groovy in Action" author= "Dierk Konig" } } }
def builder = new JsonGroovyBuilder() def books = builder.books { book { title = "Groovy in Action" author= "Dierk Konig" } book { title = "Groovy in Action" author= "Dierk Konig" } }
{ "books": { "book": [ { "title": "Groovy in Action", "author": "Dierk Konig" }, { "title": "Groovy in Action", "author": "Dierk Konig" } ] } }
[
Index
|
Groovy Support
]
JRuby Support
Creating a JSONObject
json = JSONObject.new json.element("string","json").element("bool",true) json["integer"] = 1 assert_equal "json", json["string"] assert_equal true, json["bool"] assert_equal 1, json["integer"]
[
Index
|
JRuby Support
]
Append elements to JSONObject with << (array)
json = JSONObject.new json && ["key","value"] assert !json.empty? assert_equal "value", json["key"]
[
Index
|
JRuby Support
]
Append elements to JSONObject with << (hash)
json = JSONObject.new json & {"key1" => "value1", "key2" => "value2" } assert !json.empty? assert_equal 2, json.size() assert_equal "value1", json["key1"] assert_equal "value2", json["key2"]
[
Index
|
JRuby Support
]