Conversion of JSON and Java data

Source: Internet
Author: User

JSON-lib is a Java class package used to convert beans, maps, and XML into JSON and convert JSON back to bean and dynabean.

: Http://json-lib.sourceforge.net/

The third-party package is also required:
Org. Apache. commons (version 3.2 or later)
Org. Apache. oro
Net. SF. ezmorph (ezmorph-1.0.4.jar)
Nu. XOM

1. List

Java code
  1. Boolean[] Boolarray =
    New
    Boolean[] {True,False,True};
  2. Jsonarray jsonarray1 = jsonarray. fromobject (boolarray );
  3. System. Out. println (jsonarray1 );
  4. // Prints [true, false, true]
  5. List list =NewArraylist ();
  6. List. Add ("first ");
  7. List. Add ("second ");
  8. Jsonarray jsonarray2 = jsonarray. fromobject (list );
  9. System. Out. println (jsonarray2 );
  10. // Prints ["first", "second"]
  11. Jsonarray jsonarray3 = jsonarray. fromobject ("['json', 'is', 'easy']");
  12. System. Out. println (jsonarray3 );
  13. // Prints ["JSON", "is", "easy"]
boolean[] boolArray = new boolean[]{true,false,true};JSONArray jsonArray1 = JSONArray.fromObject( boolArray );System.out.println( jsonArray1 );// prints [true,false,true]List list = new ArrayList();list.add( "first" );list.add( "second" );JSONArray jsonArray2 = JSONArray.fromObject( list );System.out.println( jsonArray2 );// prints ["first","second"]JSONArray jsonArray3 = JSONArray.fromObject( "['json','is','easy']" );System.out.println( jsonArray3 );// prints ["json","is","easy"]

2. Map

Java code
  1. Map map =NewHashmap ();
  2. Map. Put ("name ",
    "JSON ");
  3. Map. Put ("bool", Boolean. True );
  4. Map. Put ("int ",
    NewINTEGER (1 ));
  5. Map. Put ("arr ",
    NewString [] {"A", "B "}
    );
  6. Map. Put ("func ",
    "Function (I) {return this. Arr [I];}");
  7. Jsonobject JSON = jsonobject. fromobject (MAP );
  8. System. Out. println (JSON );
  9. // {"Func": function (I) {return this. arr [I] ;}, "arr": ["A", "B"], "int": 1, "name": "JSON", "bool ": true}
Map map = new HashMap();map.put( "name", "json" );map.put( "bool", Boolean.TRUE );map.put( "int", new Integer(1) );map.put( "arr", new String[]{"a","b"} );map.put( "func", "function(i){ return this.arr[i]; }" );JSONObject json = JSONObject.fromObject( map );System.out.println( json );//{"func":function(i){ return this.arr[i]; },"arr":["a","b"],"int":1,"name":"json","bool":true}

3. Bean

Java code
  1. /**
  2. * Bean. Java
  3. Private string name = "JSON ";
  4. Private int pojoid = 1;
  5. Private char [] Options = new char [] {'A', 'F '};
  6. Private string func1 = "function (I) {return this. Options [I];}";
  7. Private jsonfunction func2 = new jsonfunction (New String [] {"I"}, "return this. Options [I];");
  8. */
  9. Jsonobject = jsonobject. fromobject (NewJsonbean ());
  10. System. Out. println (jsonobject );
  11. // {"Func1": function (I) {return this. options [I];}, "pojoid": 1, "name": "JSON", "options": ["A", "F"], "func2 ": function (I) {return this. options [I] ;}}
/*** Bean.javaprivate String name = "json";private int pojoId = 1;private char[] options = new char[]{'a','f'};private String func1 = "function(i){ return this.options[i]; }";private JSONFunction func2 = new JSONFunction(new String[]{"i"},"return this.options[i];");*/JSONObject jsonObject = JSONObject.fromObject( new JsonBean() );System.out.println( jsonObject );//{"func1":function(i){ return this.options[i]; },"pojoId":1,"name":"json","options":["a","f"],"func2":function(i){ return this.options[i]; }}

4. Beans

Java code
  1. /**
  2. * Private int row;
  3. Private int Col;
  4. Private string value;
  5. *
  6. */
  7. List list =NewArraylist ();
  8. Jsonbean2 jb1 =NewJsonbean2 ();
  9. Jb1.setcol (1 );
  10. Jb1.setrow (1 );
  11. Jb1.setvalue ("XX ");
  12. Jsonbean2 jb2 =NewJsonbean2 ();
  13. Jb2.setcol (2 );
  14. Jb2.setrow (2 );
  15. Jb2.setvalue ("");
  16. List. Add (jb1 );
  17. List. Add (jb2 );
  18. Jsonarray ja = jsonarray. fromobject (list );
  19. System. Out. println (JA. tostring ());
  20. // [{"Value": "XX", "row": 1, "col": 1 },{ "value": "", "row": 2, "col": 2}]
/*** private int row ;private int col ;private String value ;**/List list = new ArrayList();JsonBean2 jb1 = new JsonBean2();jb1.setCol(1);jb1.setRow(1);jb1.setValue("xx");JsonBean2 jb2 = new JsonBean2();jb2.setCol(2);jb2.setRow(2);jb2.setValue("");list.add(jb1);list.add(jb2);JSONArray ja = JSONArray.fromObject(list);System.out.println( ja.toString() );//[{"value":"xx","row":1,"col":1},{"value":"","row":2,"col":2}]

5. string to Bean

Java code
  1. String JSON = "{name = \" JSON \ ", bool: True, INT: 1, double: 2.2, FUNC: function (a) {return a ;}, array: [1, 2]} ";
  2. Jsonobject = jsonobject. fromstring (JSON );
  3. Object bean = jsonobject. tobean (jsonobject );
  4. Assertequals (jsonobject. Get ("name"), propertyutils. getproperty (bean,
    "Name "));
  5. Assertequals (jsonobject. Get ("bool"), propertyutils. getproperty (bean,
    "Bool "));
  6. Assertequals (jsonobject. Get ("int"), propertyutils. getproperty (bean,
    "Int "));
  7. Assertequals (jsonobject. Get ("double"), propertyutils. getproperty (bean,
    "Double "));
  8. Assertequals (jsonobject. Get ("func"), propertyutils. getproperty (bean,
    "Func "));
  9. List expected = jsonarray. tolist (jsonobject. getjsonarray ("array "));
  10. Assertequals (expected, (list) propertyutils. getproperty (bean,
    "Array "));
String json = "{name=\"json\",bool:true,int:1,double:2.2,func:function(a){ return a; },array:[1,2]}";JSONObject jsonObject = JSONObject.fromString(json);Object bean = JSONObject.toBean( jsonObject );assertEquals( jsonObject.get( "name" ), PropertyUtils.getProperty( bean, "name" ) );assertEquals( jsonObject.get( "bool" ), PropertyUtils.getProperty( bean, "bool" ) );assertEquals( jsonObject.get( "int" ), PropertyUtils.getProperty( bean, "int" ) );assertEquals( jsonObject.get( "double" ), PropertyUtils.getProperty( bean, "double" ) );assertEquals( jsonObject.get( "func" ), PropertyUtils.getProperty( bean, "func" ) );List expected = JSONArray.toList( jsonObject.getJSONArray( "array" ) );assertEquals( expected, (List) PropertyUtils.getProperty( bean, "array" ) );

 

Java code
  1. String JSON = "{\" value \ ": \" XX \ ", \" Row \ ": 1, \" Col \ ": 1 }";
  2. Jsonobject = jsonobject. fromstring (JSON );
  3. Jsonbean2 bean = (jsonbean2) jsonobject. tobean (jsonobject, jsonbean2.Class);
  4. Assertequals (jsonobject. Get ("col "),NewINTEGER (bean. getcol ()));
  5. Assertequals (jsonobject. Get ("row "),
    NewINTEGER (bean. getrow ()));
  6. Assertequals (jsonobject. Get ("value"), Bean. getvalue ());
String json = "{\"value\":\"xx\",\"row\":1,\"col\":1}";JSONObject jsonObject = JSONObject.fromString(json);JsonBean2 bean = (JsonBean2) JSONObject.toBean( jsonObject, JsonBean2.class );assertEquals( jsonObject.get( "col" ),new Integer( bean.getCol())  );assertEquals( jsonObject.get( "row" ), new Integer( bean.getRow() ) );assertEquals( jsonObject.get( "value" ), bean.getValue() );

6 JSON to XML
1)
Jsonobject JSON = new jsonobject (true );
String xml = xmlserializer. Write (JSON );

<O class = "object" null = "true">

2)
Jsonobject JSON = jsonobject. fromobject ("{\" Name \ ": \" JSON \ ", \" bool \ ": True, \" int \ ": 1 }");

String xml = xmlserializer. Write (JSON );
<O class = "object">
<Name type = "string"> JSON </Name>
<Bool type = "Boolean"> true </bool>
<Int type = "Number"> 1 </int>
</O>
<O class = "object">
<Name type = "string"> JSON </Name>
<Bool type = "Boolean"> true </bool>
<Int type = "Number"> 1 </int>
</O>
3)
Jsonarray JSON = jsonarray. fromobject ("[1, 2, 3]");
String xml = xmlserializer. Write (JSON );
<A class = "array">
<E type = "Number"> 1 </E>
<E type = "Number"> 2 </E>
<E type = "Number"> 3 </E>
</A>

7. XML to JSON
<A class = "array">
<E type = "function" Params = "I, j">
Return matrix [I] [J];
</E>
</A>
<A class = "array">
<E type = "function" Params = "I, j">
Return matrix [I] [J];
</E>
</A>

Jsonarray JSON = (jsonarray) xmlserializer. Read (XML );
System. Out. println (JSON );
// Prints [function (I, j) {return matrix [I] [J];}]

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.