The use of Java background JSON data

Source: Internet
Author: User

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

Add Json-lib package to your app ... Usage above
Jsonobject jsonobject = jsonobject.fromobject (message);
GetResponse (). Getwriter (). Write (jsonobject.tostring ());

When the data is converted to JSON, it is sent to the client using the method as above. The front end is ready to get JSON data. You can also use List List1 = new arraylist<listdate> () listdate ListDate2 = new Listdate ();
Listdate2.setid (Examsubject.getid ());
Listdate2.setvalue (Examsubject.getsubjectname ());     List1.add (LISTDATE2); Jsonarray jsonArray1 = Jsonarray.fromobject (List1); The foreground loop takes $.each (date, function (i, obj) {
$ ("#examName"). Append ("<option value=" + obj.id + ">" + obj.value+ "</option>");
});

The use of Java background JSON data

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.