JSON and Java data conversion (bean, map, and XML are converted to JSON and JSON can be converted back to bean and dynabean)

Source: Internet
Author: User

Examples on the official website

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"]


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}


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] ;}}


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}]


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 "));



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 ());

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.