Resolves the jsonobject/jsonarray of nested list types into the original list problem.

Source: Internet
Author: User

Resolves an issue where the collection class member variables of an object become other types of objects during the conversion process.

public class A {

Private list<b> blist;

Public A (list<b> temp) {

blist = temp;

}

public void Setblist (list<b> temp) {

blist = temp;

}

  

Public list<b> getblist () {

return blist;

}

}

public class mainclass{

public static void Main (String args[]) {

List <A> al = new Arraylist<a> ();

List <B> bl = new arraylist<b> ();

Bl.add (New B ());

Al.add (new A (BL));

Prevent the cycle of death

Jsonconfig jsonconfig = new Jsonconfig ();
Jsonconfig.setcycledetectionstrategy (cycledetectionstrategy.lenient);

Into the jsonarray of the ToString to the front desk.

String result = Jsonarray.fromobject (AL, Jsonconfig). toString ();

Front desk stringify array, upload to backstage and change back

Jsonconfig jsonconfig = new Jsonconfig ();
Jsonconfig.setcycledetectionstrategy (cycledetectionstrategy.lenient);
Jsonarray Jsonarray = jsonarray.fromobject (result);
list<a> al1= new arraylist<a> ();

It is not possible to use jsonarray.tocollection () directly in the collection class, and then set it to the parent class, otherwise it becomes a different object.
for (int i = 0; i < jsonarray.size (); i++) {
Jsonobject json = Jsonarray.getjsonobject (i);
Jsonarray blist = Json.getjsonarray ("blist");
@SuppressWarnings ("Unchecked")
List<b> blist= (list<b>) jsonarray.tocollection (blist, B.class);
A ob1 = (groupproperty) Jsonobject.tobean (JSON, a.class);
Ob1.setblist (blist);
Al1.add (OB1);

}

Al1 's right here.

System.out.println (AL1);

    

}

}

Resolves the jsonobject/jsonarray of nested list types into the original list problem.

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.