Json-lib Bag Notes

Source: Internet
Author: User

Json-lib.jar Development Kit Uses:

Dependent Packages:
Commons-beanutils.jar;
Commons-httpclient.jar;
Commons-lang.jar;
Ezmorph.jar, a lot of people use will prompt net.sf.ezmorph.xxx can not find, is missing this:
Morph-1.0.1.jar

RELATED Links:
http://json-lib.sourceforge.net/
http://ezmorph.sourceforge.net/
http://morph.sourceforge.net/

problems during use:
1, the old hint of the following error when converting beans to JSON format:
Exception in thread "main" Net.sf.json.JSONException:java.lang.NoSuchMethodException:Property ' name ' have no getter meth Od
FIX: Declare bean is public class XXX, must be public, I do not use the default type (class XXX)

2,exception in thread "main" Java.lang.NoSuchMethodError:org.apache.commons.lang.ArrayUtils.toObject ([C) [ljava/ Lang/character;
Reason: The definition attribute is as follows: Private char[] options = new char[] {' A ', ' f '}; It seems that this type of

3, private String func1 = "function (i) {return this.options[i];}";
And
Private Jsonfunction Func2 = new Jsonfunction (new string[] {"I"},
"Return this.options[i];");
The results are similar after conversion:
{"Func1": function (i) {return this.options[i];, "Func2": function (i) {return this.options[i];}}

Test class:
  1. import java.util.ArrayList;
  2. import Java.util.HashMap;
  3. import java.util.List;
  4. import Java.util.Map;
  5. import Net.sf.json.JSONArray;
  6. import Net.sf.json.JSONObject;
  7. Public class Json {
  8. Public Static void Main (string[] args) {
  9. JSON j = New json ();
  10. J.bean2json ();
  11. }
  12. Public void Arr2json () {
  13. Boolean [] Boolarray = new boolean[] { true, false, true };
  14. Jsonarray Jsonarray = Jsonarray.fromobject (Boolarray);
  15. System.out.println (Jsonarray);
  16. Prints [true,false,true]
  17. }
  18. Public void List2json () {
  19. List List = new ArrayList ();
  20. List.add ("first");
  21. List.add ("second");
  22. Jsonarray Jsonarray = jsonarray.fromobject (list);
  23. System.out.println (Jsonarray);
  24. Prints ["First", "second"]
  25. }
  26. Public void Createjson () {
  27. Jsonarray Jsonarray = Jsonarray.fromobject ("[' json ', ' is ', ' easy ']");
  28. System.out.println (Jsonarray);
  29. Prints ["JSON", "is", "easy"]
  30. }
  31. Public void Map2json () {
  32. Map
  33. Map.put ("name", "JSON");
  34. Map.put ("bool", boolean.true);
  35. Map.put ("int", new Integer (1));
  36. Map.put ("arr", new string[] {"A", "B"});
  37. Map.put ("func", "function (i) {return this.arr[i];}");
  38. Jsonobject JSON = jsonobject.fromobject (map);
  39. SYSTEM.OUT.PRINTLN (JSON);
  40. Prints
  41. ["Name": "JSON", "bool": TRUE, "int": 1, "arr": ["a", "B"], "func": function (i) {
  42. return this.arr[i]; }]
  43. }
  44. Public void Bean2json () {
  45. Jsonobject jsonobject = Jsonobject.fromobject (new Mybean ());
  46. System.out.println (Jsonobject);
  47. /*
  48. * Prints
  49. * {"func1": function (i) {return this.options[i];
  50. *}, "pojoid": 1, "name": "JSON", "FUNC2": function (i) {return
  51. * This.options[i]; }}
  52. */
  53. }
  54. Public void Json2bean () {
  55. String json = "{name=/" json2/", Func1:true,pojoid:1,func2:function (a) {return A;},options:[' 1 ', ' 2 ']}";
  56. Jsonobject JB = jsonobject.fromstring (JSON);
  57. Jsonobject.tobean (JB, Mybean. Class);
  58. System.out.println ();
  59. }
  60. }

Bean to manipulate:
  1. import net.sf.json.JSONFunction;
  2. Public class Mybean {
  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"},
  8. "Return this.options[i];");
  9. Getters & Setters
  10. ......
  11. }

Use Methods of JSON

JSON, or JavaScript Object natation, is a lightweight data interchange format that is ideal for server-to-javascript interaction. This article will quickly explain the JSON format and demonstrate how to handle JSON-formatted data on both the client and server side, using code examples.

JSON-Required Packages

Commons-httpclient-3.1.jar
Commons-lang-2.4.jar
Commons-logging-1.1.1.jar
Json-lib-2.2.3-jdk13.jar
Ezmorph-1.0.6.jar
Commons-collections-3.2.1.jar

The above package can be

Http://commons.apache.org/index.html

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

http://ezmorph.sourceforge.net/

http://morph.sourceforge.net/

http://www.docjar.com/

Downloaded to the.

The Java.lang.noclassdeffounderror:net/sf/ezmorph/morpher error occurred because the Ezmorph.jar file was not imported or the version is incorrect.

Appears java.lang.noclassdeffounderror:org/apache/commons/collections/map/ The LISTORDEREDMAP error is because the Commons-collections.jar file is not imported or the version is incorrect.

Translating Java code into JSON code

1. The list collection is converted into JSON code

List List = new ArrayList ();

List.add ("first");

List.add ("second");

Jsonarray jsonArray2 = Jsonarray. Fromobject (list);

2. Map collection into JSON code

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

3. Bean converted to JSON code

Jsonobject jsonobject = jsonobject.fromobject (New Jsonbean ());

4. Convert arrays into JSON code

Boolean [] Boolarray = new boolean[] { true, false, true };

Jsonarray jsonArray1 = Jsonarray. Fromobject (Boolarray);

5. Conversion of general data into JSON code

Jsonarray jsonArray3 = Jsonarray.fromobject ("[' json ', ' is ', ' easy ']");

6. Beans conversion to JSON code

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

Json-lib Bag Notes

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.