Four common scenarios in which Java object JSON data is converted to each other

Source: Internet
Author: User

1. Convert the list of Java objects to an array of JSON objects and move to a string

Jsonarray array = jsonarray.fromobject (userlist);
String jsonstr = array.tostring ();

2. Converting Java objects to JSON objects and converting them into strings

Jsonobject Object = Jsonobject.fromobject (invite);
String str=object.tostring ());

3. Convert the JSON string to an array of Java objects

String personstr = Getrequest (). GetParameter ("persons");
Jsonarray json = Jsonarray.fromobject (PERSONSTR);
list<invoidperson> persons = (list<invoidperson>) jsonarray.tocollection (JSON, nvoidperson.class);

4. Convert the JSON string to a Java object

Jsonobject jsonobject = Jsonobject.fromobject (str);
Passportlendsentity passportlends = null;
try {
Get a JSON array
Jsonarray array = Jsonobject.getjsonarray ("passports");
Convert JSON arrays to list<passportforlendsentity> generics
list<passportforlendsentity> list = new arraylist<passportforlendsentity> ();
for (int i = 0; i < array.size (); i++) {
Jsonobject object = (jsonobject) array.get (i);
Passportforlendsentity Passport = (passportforlendsentity) Jsonobject.tobean (object,
Passportforlendsentity.class);
if (passport! = null) {
List.add (Passport);
}
}
Convert passportlendsentity entity class
Passportlends = (passportlendsentity) Jsonobject.tobean (Jsonobject, Passportlendsentity.class);

str = "{\" lendperson\ ": \" John Doe \ ", \" lendcompany\ ": \" limited \ ", \" checkperson\ ": \" John Doe \ ",

\ "lenddate\": \ "2010-07-19t00:00:00\", \ "lendcounts\": 4,\ "
Passports\ ": [{\" passportid\ ": \" d\ ", \" name\ ": \" Li Shuichang \ ", \" passporttype\ ": \" K\ "},
{\ "passportid\": \ "k9051\", \ "name\": \ "Li ping \", \ "passporttype\": \ "K\"},
{\ "passportid\": \ "k90517\", \ "name\": \ "Yuan Han mei \", \ "passporttype\": \ "K\"},
{\ "passportid\": \ "k905199\", \ "name\": \ "He Ming \", \ "passporttype\": \ "K\"}]} ";

Four common scenarios in which Java object JSON data is converted to each other

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.