`
huanglanfa
  • 浏览: 21220 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
文章分类
社区版块
存档分类
最新评论

JSON to java

阅读更多
From JSON to Java
1.JSONObject to DynaBean
2.JSONObject to JavaBean
3.JSONArray to List
4.JSONArray to array
5.JSONArray to array (type conversion)
6.JSONObject to JavaBean, exclude properties with filters
From JSON to XML
1.JSONObject to XML
2.JSONObject to XML (no type hints)
3.JSONObject to XML (with json prefix)
4.JSONObject to XML (change node names)
5.JSONArray to XML
6.JSONArray to XML (no type hints)
7.JSONArray to XML (with json prefix)
8.Flatten JSONArray into parent
From XML to JSON
1.XML to JSONObject
Groovy Support
1.Using the JsonGroovyBuilder
JRuby Support
1.Creating a JSONObject
2.Append elements to JSONObject with << (array)
3.Append elements to JSONObject with << (hash)
From Java to JSON
Creating a JSONObject from scratch
1.JSONObject jsonObject = new JSONObject() 
2.                              .element( "string", "JSON" ) 
3.                              .element( "integer", "1" ) 
4.                              .element( "double", "2.0" ) 
5.                              .element( "boolean", "true" ); 
6.assertEquals( "JSON", jsonObject.getString("string") );       
7.assertEquals( 1, jsonObject.getInt("integer") );       
8.assertEquals( 2.0d, jsonObject.getDouble("double"), 0d );       
9.assertTrue( jsonObject.getBoolean("boolean") );       
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
1.String str = "{'string':'JSON', 'integer': 1, 'double': 2.0, 'boolean': true}"; 
2.JSONObject jsonObject = (JSONObject) JSONSerializer.toJSON( str ); 
3.assertEquals( "JSON", jsonObject.getString("string") );       
4.assertEquals( 1, jsonObject.getInt("integer") );       
5.assertEquals( 2.0d, jsonObject.getDouble("double"), 0d );       
6.assertTrue( jsonObject.getBoolean("boolean") );       
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
1.Map map = new HashMap(); 
2.map.put( "string", "JSON" ); 
3.map.put( "integer", "1" ); 
4.map.put( "double", "2.0" ); 
5.map.put( "boolean", "true" ); 
6.JSONObject jsonObject = (JSONObject) JSONSerializer.toJSON( map ); 
7.assertEquals( "JSON", jsonObject.getString("string") );       
8.assertEquals( 1, jsonObject.getInt("integer") );       
9.assertEquals( 2.0d, jsonObject.getDouble("double"), 0d );       
10.assertTrue( jsonObject.getBoolean("boolean") );       
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
1.public class MyJavaBean { 
2.   private String string; 
3.   private int integer; 
4.   private double dooble; 
5.   private boolean bool; 
6. 
7.   // getters & setters 
8.} 
9. 
10.MyJavaBean bean = new MyJavaBean(); 
11.bean.setString( "JSON" ); 
12.bean.setInteger( 1 ); 
13.bean.setDooble( 2.0d ); 
14.bean.setBool( true ); 
15.JSONObject jsonObject = (JSONObject) JSONSerializer.toJSON( bean ); 
16.assertEquals( "JSON", jsonObject.getString("string") );       
17.assertEquals( 1, jsonObject.getInt("integer") );       
18.assertEquals( 2.0d, jsonObject.getDouble("dooble"), 0d );       
19.assertTrue( jsonObject.getBoolean("bool") );       
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
1.JSONArray jsonArray = new JSONArray() 
2.                              .element( "JSON" ) 
3.                              .element( "1" ) 
4.                              .element( "2.0" ) 
5.                              .element( "true" ); 
6.assertEquals( "JSON", jsonArray.getString(0) );       
7.assertEquals( 1, jsonArray.getInt(1) );       
8.assertEquals( 2.0d, jsonArray.getDouble(2), 0d );       
9.assertTrue( jsonArray.getBoolean(3) );       
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
1.String str = "['JSON', 1, 2.0, true]"; 
2.JSONArray jsonArray = (JSONArray) JSONSerializer.toJSON( str ); 
3.assertEquals( "JSON", jsonArray.getString(0) );       
4.assertEquals( 1, jsonArray.getInt(1) );       
5.assertEquals( 2.0d, jsonArray.getDouble(2), 0d );       
6.assertTrue( jsonArray.getBoolean(3) );       
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
1.List list = new ArrayList(); 
2.list.add( "JSON" ); 
3.list.add( "1" ); 
4.list.add( "2.0" ); 
5.list.add( "true" ); 
6.JSONArray jsonArray = (JSONArray) JSONSerializer.toJSON( list ); 
7.assertEquals( "JSON", jsonArray.getString(0) );       
8.assertEquals( 1, jsonArray.getInt(1) );       
9.assertEquals( 2.0d, jsonArray.getDouble(2), 0d );       
10.assertTrue( jsonArray.getBoolean(3) );       
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
1.Object[] array = new Object[]{ "JSON", "1", "2.0", "true" }; 
2.JSONArray jsonArray = (JSONArray) JSONSerializer.toJSON( array ); 
3.assertEquals( "JSON", jsonArray.getString(0) );       
4.assertEquals( 1, jsonArray.getInt(1) );       
5.assertEquals( 2.0d, jsonArray.getDouble(2), 0d );       
6.assertTrue( jsonArray.getBoolean(3) );       
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
1.String str = "{'func': function( param ){ doSomethingWithParam(param); }}"; 
2.JSONObject jsonObject = (JSONObject) JSONSerializer.toJSON( str ); 
3.JSONFunction func = (JSONFunction) jsonObject.get("func"); 
4.ArrayAssertions.assertEquals( new String[]{"param"}, func.getParams() ); 
5.assertEquals( "doSomethingWithParam(param);", func.getText() ); 
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
1.String str = "{'string':'JSON', 'integer': 1, 'double': 2.0, 'boolean': true}"; 
2.JsonConfig jsonConfig = new JsonConfig(); 
3.jsonConfig.setExcludes( new String[]{ "double", "boolean" } ); 
4.JSONObject jsonObject = (JSONObject) JSONSerializer.toJSON( str, jsonConfig ); 
5.assertEquals( "JSON", jsonObject.getString("string") );       
6.assertEquals( 1, jsonObject.getInt("integer") );       
7.assertFalse( jsonObject.has("double") );    
8.assertFalse( jsonObject.has("boolean") );    
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)
1.String str = "{'string':'JSON', 'integer': 1, 'double': 2.0, 'boolean': true}"; 
2.JsonConfig jsonConfig = new JsonConfig(); 
3.jsonConfig.setJsonPropertyFilter( new PropertyFilter(){   
4.   public boolean apply( Object source, String name, Object value ) {   
5.      if( "double".equals(value) || "boolean".equals(value) ){   
6.         return true;   
7.      }   
8.      return false;   
9.   }   
10.});   
11.JSONObject jsonObject = (JSONObject) JSONSerializer.toJSON( str, jsonConfig ); 
12.assertEquals( "JSON", jsonObject.getString("string") );       
13.assertEquals( 1, jsonObject.getInt("integer") );       
14.assertFalse( jsonObject.has("double") );    
15.assertFalse( jsonObject.has("boolean") );    
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
1.String str = "{'string':'JSON', 'integer': 1, 'double': 2.0, 'boolean': true}"; 
2.JSONObject jsonObject = (JSONObject) JSONSerializer.toJSON( str ); 
3.DynaBean bean = (DynaBean) JSONSerializer.toJava( jsonObject ); 
4.assertEquals( "JSON", bean.get("string") );       
5.assertEquals( new Integer(1), bean.get("integer") );       
6.assertEquals( new Double(2.0), bean.get("double") );       
7.assertEquals( Boolean.TRUE, bean.get("boolean") );    
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
1.public class MyJavaBean { 
2.   private String string; 
3.   private int integer; 
4.   private double dooble; 
5.   private boolean bool; 
6. 
7.   // getters & setters 
8.} 
9. 
10.MyJavaBean bean = new MyJavaBean(); 
11.bean.setString( "JSON" ); 
12.bean.setInteger( 1 ); 
13.bean.setDooble( 2.0d ); 
14.bean.setBool( true ); 
15.JSONObject jsonObject = (JSONObject) JSONSerializer.toJSON( bean ); 
16.JsonConfig jsonConfig = new JsonConfig(); 
17.jsonConfig.setRootClass( MyJavaBean.class ); 
18.MyJavaBean bean2 = (MyJavaBean) JSONSerializer.toJava( jsonObject, jsonConfig ); 
19.assertNotNull( bean2 ); 
20.assertEquals( "JSON", bean2.getString() ); 
21.assertEquals( 1, bean2.getInteger() ); 
22.assertEquals( 2.0d, bean2.getDooble(), 0d ); 
23.assertTrue( bean2.getBool() ); 
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
1.List input = new ArrayList(); 
2.input.add( "JSON" ); 
3.input.add( "1" ); 
4.input.add( "2.0" ); 
5.input.add( "true" ); 
6.JSONArray jsonArray = (JSONArray) JSONSerializer.toJSON( input ); 
7.List output = (List) JSONSerializer.toJava( jsonArray ); 
8.ArrayAssertions.assertEquals( input, output ); 
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
1.List input = new ArrayList(); 
2.input.add( "JSON" ); 
3.input.add( "1" ); 
4.input.add( "2.0" ); 
5.input.add( "true" ); 
6.JSONArray jsonArray = (JSONArray) JSONSerializer.toJSON( input ); 
7.JsonConfig jsonConfig = new JsonConfig(); 
8.jsonConfig.setArrayMode( JsonConfig.MODE_OBJECT_ARRAY ); 
9.Object[] output = (Object[]) JSONSerializer.toJava( jsonArray, jsonConfig ); 
10.Object[] expected = new Object[]{ "JSON", "1", "2.0", "true" }; 
11.ArrayAssertions.assertEquals( expected, output); 
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)
1.List input = new ArrayList(); 
2.input.add( "1" ); 
3.input.add( "2" ); 
4.input.add( "3.0" ); 
5.JSONArray jsonArray = (JSONArray) JSONSerializer.toJSON( input ); 
6.JsonConfig jsonConfig = new JsonConfig(); 
7.jsonConfig.setArrayMode( JsonConfig.MODE_OBJECT_ARRAY ); 
8.jsonConfig.setRootClass( Integer.TYPE ); 
9.int[] output = (int[]) JSONSerializer.toJava( jsonArray, jsonConfig ); 
10.int[] expected = new int[]{ 1, 2, 3 }; 
11.ArrayAssertions.assertEquals( expected, output); 
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
1.public class MyJavaBean { 
2.   private String string; 
3.   private int integer; 
4.   private double dooble = 0d; 
5.   private boolean bool; 
6. 
7.   // getters & setters 
8.} 
9. 
10.MyJavaBean bean = new MyJavaBean(); 
11.bean.setString( "JSON" ); 
12.bean.setInteger( 1 ); 
13.bean.setDooble( 2.0d ); 
14.bean.setBool( true ); 
15.JSONObject jsonObject = (JSONObject) JSONSerializer.toJSON( bean ); 
16.JsonConfig jsonConfig = new JsonConfig(); 
17.jsonConfig.setRootClass( MyJavaBean.class ); 
18.jsonConfig.setJavaPropertyFilter( new PropertyFilter(){   
19.   public boolean apply( Object source, String name, Object value ) {   
20.      if( "bool".equals( name ) || "dooble".equals( name ) ){   
21.         return true;   
22.      }   
23.      return false;   
24.   }   
25.});  
26.MyJavaBean bean2 = (MyJavaBean) JSONSerializer.toJava( jsonObject, jsonConfig ); 
27.assertNotNull( bean2 ); 
28.assertEquals( "JSON", bean2.getString() ); 
29.assertEquals( 1, bean2.getInteger() ); 
30.assertEquals( 0d, bean2.getDooble(), 0d ); 
31.assertFalse( bean2.getBool() ); 
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
1.String str = "{'name':'JSON','integer':1,'double':2.0,'boolean':true,'nested':{'id':42},'array':[1,2,3]}"; 
2.JSON json = JSONSerializer.toJSON( str ); 
3.XMLSerializer xmlSerializer = new XMLSerializer(); 
4.String xml = xmlSerializer.write( json ); 
5.System.out.println(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);

1.<?xml version="1.0" encoding="UTF-8"?> 
2.<o> 
3.   <boolean type="boolean">true</boolean> 
4.   <double type="number">2.0</double> 
5.   <integer type="number">1</integer> 
6.   <name type="string">JSON</name> 
7.   <nested class="object"> 
8.      <id type="number">42</id> 
9.   </nested> 
10.   <array class="array"> 
11.      <e type="number">1</e> 
12.      <e type="number">2</e> 
13.      <e type="number">3</e> 
14.   </array> 
15.</o> 
<?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)
1.String str = "{'name':'JSON','integer':1,'double':2.0,'boolean':true,'nested':{'id':42},'array':[1,2,3]}"; 
2.JSON json = JSONSerializer.toJSON( str ); 
3.XMLSerializer xmlSerializer = new XMLSerializer(); 
4.xmlSerializer.setTypeHintsEnabled( false ); 
5.String xml = xmlSerializer.write( json ); 
6.System.out.println(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();
xmlSerializer.setTypeHintsEnabled( false );
String xml = xmlSerializer.write( json );
System.out.println(xml);

1.<?xml version="1.0" encoding="UTF-8"?> 
2.<o> 
3.   <boolean>true</boolean> 
4.   <double>2.0</double> 
5.   <integer>1</integer> 
6.   <name>JSON</name> 
7.   <nested> 
8.      <id>42</id> 
9.   </nested> 
10.   <array> 
11.      <e>1</e> 
12.      <e>2</e> 
13.      <e>3</e> 
14.   </nested> 
15.</o> 
<?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)
1.String str = "{'name':'JSON','integer':1,'double':2.0,'boolean':true,'nested':{'id':42},'array':[1,2,3]}"; 
2.JSON json = JSONSerializer.toJSON( str ); 
3.XMLSerializer xmlSerializer = new XMLSerializer(); 
4.xmlSerializer.setTypeHintsCompatibility( false ); 
5.String xml = xmlSerializer.write( json ); 
6.System.out.println(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();
xmlSerializer.setTypeHintsCompatibility( false );
String xml = xmlSerializer.write( json );
System.out.println(xml);

1.<?xml version="1.0" encoding="UTF-8"?> 
2.<o> 
3.   <boolean json_type="boolean">true</boolean> 
4.   <double json_type="number">2.0</double> 
5.   <integer json_type="number">1</integer> 
6.   <name json_type="string">JSON</name> 
7.   <nested json_class="object"> 
8.      <id json_type="number">42</id> 
9.   </nested> 
10.   <array json_class="array"> 
11.      <e json_type="number">1</e> 
12.      <e json_type="number">2</e> 
13.      <e json_type="number">3</e> 
14.   </array> 
15.</o> 
<?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)
1.String str = "{'name':'JSON','integer':1,'double':2.0,'boolean':true,'nested':{'id':42},'array':[1,2,3]}"; 
2.JSON json = JSONSerializer.toJSON( str ); 
3.XMLSerializer xmlSerializer = new XMLSerializer(); 
4.xmlSerializer.setRootName( "root" ); 
5.xmlSerializer.setElementName( "element" ); 
6.String xml = xmlSerializer.write( json ); 
7.System.out.println(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();
xmlSerializer.setRootName( "root" );
xmlSerializer.setElementName( "element" );
String xml = xmlSerializer.write( json );
System.out.println(xml);

1.<?xml version="1.0" encoding="UTF-8"?> 
2.<root> 
3.   <boolean type="boolean">true</boolean> 
4.   <double type="number">2.0</double> 
5.   <integer type="number">1</integer> 
6.   <name type="string">JSON</name> 
7.   <nested class="object"> 
8.      <id type="number">42</id> 
9.   </nested> 
10.   <array class="array"> 
11.      <element type="number">1</e> 
12.      <element type="number">2</e> 
13.      <element type="number">3</e> 
14.   </array> 
15.</root> 
<?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
1.String str = "['JSON', 1, 2.0, true, {'id':42}, [1,2,3]]"; 
2.JSON json = JSONSerializer.toJSON( str ); 
3.XMLSerializer xmlSerializer = new XMLSerializer(); 
4.String xml = xmlSerializer.write( json ); 
5.System.out.println(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);

1.<?xml version="1.0" encoding="UTF-8"?> 
2.<a> 
3.   <e type="string">JSON</e> 
4.   <e type="number">1</e> 
5.   <e type="number">2.0</e> 
6.   <e type="boolean">true</e> 
7.   <e class="object"> 
8.      <id type="number">42</id> 
9.   </e> 
10.   <e class="array"> 
11.      <e type="number">1</e> 
12.      <e type="number">2</e> 
13.      <e type="number">3</e> 
14.   </e> 
15.</a> 
<?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)
1.String str = "['JSON', 1, 2.0, true, {'id':42}, [1,2,3]]"; 
2.JSON json = JSONSerializer.toJSON( str ); 
3.XMLSerializer xmlSerializer = new XMLSerializer(); 
4.xmlSerializer.setTypeHintsEnabled( false ); 
5.String xml = xmlSerializer.write( json ); 
6.System.out.println(xml); 
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);

1.<?xml version="1.0" encoding="UTF-8"?> 
2.<a> 
3.   <e>JSON</e> 
4.   <e>1</e> 
5.   <e>2.0</e> 
6.   <e>true</e> 
7.   <e> 
8.      <id>42</id> 
9.   </e> 
10.   <e> 
11.      <e>1</e> 
12.      <e>2</e> 
13.      <e>3</e> 
14.   </e> 
15.</a> 
<?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)
1.String str = "['JSON', 1, 2.0, true, {'id':42}, [1,2,3]]"; 
2.JSON json = JSONSerializer.toJSON( str ); 
3.XMLSerializer xmlSerializer = new XMLSerializer(); 
4.xmlSerializer.setTypeHintsCompatibility( false ); 
5.String xml = xmlSerializer.write( json ); 
6.System.out.println(xml); 
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);

1.<?xml version="1.0" encoding="UTF-8"?> 
2.<a> 
3.   <e json_type="string">JSON</e> 
4.   <e json_type="number">1</e> 
5.   <e json_type="number">2.0</e> 
6.   <e json_type="boolean">true</e> 
7.   <e json_class="object"> 
8.      <id json_type="number">42</id> 
9.   </e> 
10.   <e json_class="array"> 
11.      <e json_type="number">1</e> 
12.      <e json_type="number">2</e> 
13.      <e json_type="number">3</e> 
14.   </e> 
15.</a> 
<?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
1.String str = "{'number': [1,2,3]}"; 
2.JSONObject json = (JSONObject) JSONSerializer.toJSON( str ); 
3.XMLSerializer xmlSerializer = new XMLSerializer(); 
4.xmlSerializer.setTypeHintsEnabled( false ); 
5.String xml = xmlSerializer.write( json ); 
6.System.out.println( xml ); 
7.json.getJSONArray( "number" ).setExpandElements( true ); 
8.xml = xmlSerializer.write( json ); 
9.System.out.println( xml ); 
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 );

1.<?xml version="1.0" encoding="UTF-8"?> 
2.<o> 
3.   <number> 
4.      <e>1</e> 
5.      <e>2</e> 
6.      <e>3</e> 
7.   </number> 
8.</o> 
9. 
10.<?xml version="1.0" encoding="UTF-8"?> 
11.<o> 
12.   <number>1</number> 
13.   <number>2</number> 
14.   <number>3</number> 
15.</o> 
<?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
1.<o number="1"> 
2.   first 
3.   <string>json</string> 
4.   <array> 
5.      <e>1</e> 
6.      <e>true</e> 
7.   </array> 
8.</o> 
<o number="1">
   first
   <string>json</string>
   <array>
      <e>1</e>
      <e>true</e>
   </array>
</o>

1.String xml = ... 
2.XMLSerializer xmlSerializer = new XMLSerializer(); 
3.JSON json = xmlSerializer.read( xml ); 
4.System.out.println( json.toString(2) ); 
String xml = ...
XMLSerializer xmlSerializer = new XMLSerializer();
JSON json = xmlSerializer.read( xml );
System.out.println( json.toString(2) );

1.{ 
2.  "string": "json", 
3.  "#text": "first", 
4.  "array":   [ 
5.    "1", 
6.    "true" 
7.  ], 
8.  "@number": "1" 
9.} 


本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/anyoneking/archive/2008/11/13/3291883.aspx
分享到:
评论

相关推荐

    json格式转换为java实体类,idea导入即可

    3. 使用IDEA的快捷键或找到插件入口,通常是在“Code”菜单下,选择“Convert JSON to Java Class”或类似选项。 4. 粘贴JSON数据到弹出的输入框中。 5. 设置好实体类的相关配置,如类名、是否生成getter/setter方法...

    JSON到Java对象「JSON to Java object」-crx插件

    这个扩展允许你把JSON转换成POJO,所以你可以把它用作...此扩展旨在通过允许您从明确定义的JSON字符串中生成普通的旧Java对象来提高开发人员的生产力。简化手动编写getter和setter的普通过程。 支持语言:English (UK)

    Json与java对象之间相互转换源码

    String jsonString = gson.toJson(person); ``` 将JSON字符串反序列化为Java对象: ```java Person person = gson.fromJson(jsonString, Person.class); ``` 2. **Jackson转换:** Jackson是另一个流行的...

    java 解析 json文件 java 解析 json文件

    Java解析JSON文件是Java开发中常见的一项任务,特别是在与Web服务交互或处理API响应时。JSON(JavaScript Object Notation)是一种轻量级的数据交换格式,因其简洁和易于阅读及编写的特点,被广泛应用于网络数据传输...

    JSON-java-master.zip

    JSONObject cannot be resolved to a type 是因为缺少对应的依赖包:org-json.rar 包的来源github的项目:https://github.com/stleary/JSON-java中的Readme可以找到依赖包的地址。

    json in java.rar

    String json = gson.toJson(user); // 将Java对象转换为JSON User parsedUser = gson.fromJson(json, User.class); // 将JSON反序列化回Java对象 } ``` 此外,`org.json`库提供了简单的API来处理JSON,如`...

    Java中使用Json

    String jsonString = gson.toJson(yourJavaObject); YourJavaClass yourObject = gson.fromJson(jsonString, YourJavaClass.class); ``` 除了基本的序列化和反序列化,这两个库还支持更高级的功能,如类型转换...

    JAVA-JSON工具转换类

    可能包含的方法有`toJson()`(将Java对象转换为JSON字符串)、`fromJson()`(将JSON字符串解析为Java对象)、`convertToMap()`(将JSON字符串转换为Map)以及`convertToList()`(将JSON字符串转换为List)等。...

    JAVA中用到的json包

    `JSONArray`类提供了`toArray()`方法将JSON数组转换为Java数组,而`toList()`则可以将其转换为List。 3. **键值对的处理** - 在JSONObject中,可以通过`get()`方法获取指定键的值,`opt()`方法用于安全地获取值...

    Json2Java.rar

    4. 序列化:使用`Gson`的`toJson`方法。 示例代码: ```java import com.google.gson.Gson; public class User { private String name; private int age; // getters and setters... public static void ...

    使用json-lib实现json与Java对象的互转

    `json-lib` 是一个 Java 库,它提供了将 JSON 格式的数据转换为 Java 对象,以及将 Java 对象转换为 JSON 格式数据的功能。在 Java 开发中,`json-lib` 是一个常用的 JSON 处理工具,尤其适用于需要处理复杂 JSON ...

    JSON-java-master

    String json = gson.toJson(obj); MyClass obj = gson.fromJson(jsonString, MyClass.class); ``` Fastjson是阿里巴巴的一个高性能的JSON库,它的使用方式如下: ```java String json = JSON.toJSONString(obj); ...

    JSON与Java互相转换Demo(Eclipse)

    - `Gson.toJson(Object)`:将Java对象转换为JSON字符串。 - `Gson.fromJson(String, Class)`:将JSON字符串转换为指定类型的Java对象。 在Eclipse中,你可以创建一个工具类,如`JsonUtil.java`,来封装这些转换...

    json与java对象互转

    String jsonString = gson.toJson(bean); ``` 其中,`setDateFormat(DateFormat.LONG)`设置日期格式,`setPrettyPrinting()`则使输出的JSON字符串更易读。 ##### 将JSON字符串转换为Java对象 反之,如果我们要将...

    JSON与JAVA数据的转换

    例如,我们可以创建一个Java对象,然后使用`Gson().toJson()`方法将其转换为JSON字符串。同样,我们也可以通过`Gson().fromJson()`将JSON字符串解析为对应的Java对象。 2. Jackson库:Jackson是另一个广泛使用的...

    java解析DWG文件为json使用superMap

    【标题】:“java解析DWG文件为json使用superMap” 【描述】:“java使用superMap解析DWG文件为json输出,本地通过,附件中为word操方式按照步骤可以导入数据集” 【标签】:“java解析DWG文件json java读取dwg...

    java服务端生成json(json服务端)

    String json = gson.toJson(user); // 将Java对象转换为JSON字符串 System.out.println(json); // 输出:{"name":"John","age":30} User deserializedUser = gson.fromJson(json, User.class); // 将JSON字符串...

    java-json.jar.zip

    - 调用转换方法,如`String xmlStr = JsonToXml.convert(jsonStr)`,将JSON字符串转换为XML字符串。 需要注意的是,XML和JSON的结构并不完全对应,因此在转换过程中可能会遇到一些挑战。例如,XML的命名空间在JSON...

    java json api,json api

    例如,对于一个JavaBean,你可以使用`JSONUtils.toJSON(yourBean)`将其转换为JSON对象,然后用`JSONUtils.fromObject(jsonObj, YourBeanClass.class)`将其转换回来。 4. **JSON与XML的互转**:`json-lib`还提供了将...

    json to csv最新代码

    json to csv最新代码json to csv最新代码json to csv最新代码json to csv最新代码json to csv最新代码json to csv最新代码json to csv最新代码json to csv最新代码json to csv最新代码json to csv最新代码json to ...

Global site tag (gtag.js) - Google Analytics