JSON to Java object and list collection

Source: Internet
Author: User

 Public Static voidMain (string[] args) {//Converting ObjectsString Strjson = "{\" basemenu_id\ ": \" 1\ ", \" basemenu_name\ ": \" aaaaaa\ ", \" Basemenu_order\ ": \" 2\ "}"; AAAA JB= (AAAA) json2object (strjson,aaaa.class);//JavaBean to add ". Class"System.out.print ("JSON to Object" +jb.getbasemenu_id () + "," +jb.getbasemenu_name () + "," +jb.getbasemenu_order () + "\ n"); //go to listString json= "[{\" basemenu_id\ ": \" 1\ ", \" basemenu_name\ ": \" aaaaaa\ ", \" Basemenu_order\ ": \" 2\ "}," + "{\" basemenu_id\ " : \ "2\", \ "basemenu_name\": \ "cccccc\", \ "Basemenu_order\": \ "3\"}, "+" {\ "basemenu_id\": \ "3\", \ "basemenu_name\": \ " Dddddd\ ", \" Basemenu_order\ ": \" 4\ "}," + "{\" basemenu_id\ ": \" 4\ ", \" basemenu_name\ ": \" bbbbbb\ ", \" Basemenu_order\ ": \ "5\"}, "+" {\ "basemenu_id\": \ "5\", \ "basemenu_name\": \ "eeeeee\", \ "Basemenu_order\": \ "6\"}, "+" {\ "basemenu_id\": \ "6 \ ", \" basemenu_name\ ": \" rrrrrr\ ", \" Basemenu_order\ ": \" 7\ "}," + "{\" basemenu_id\ ": \" 7\ ", \" basemenu_name\ ": \" Tttttt \ ", \" Basemenu_order\ ": \" 8\ "}," + "{\" basemenu_id\ ": \" 8\ ", \" basemenu_name\ ": \" uuuuuu\ ", \" Basemenu_order\ ": \" 9\ "}, "+" {\ "basemenu_id\": \ "9\", \ "basemenu_name\": \ "oooooo\", \ "Basemenu_order\": \ "10\"}, "+" {\ "basemenu_id\": \ "10\", \ "basemenu_name\": \ "llllll\", \ "Basemenu_order\": \ "11\"}] "; @SuppressWarnings ("Rawtypes") List List= (List) json2objectlist (JSON, AAAA.)class); @SuppressWarnings ("Rawtypes") Iterator It=List.iterator (); System.out.print ("Collection loop \ n");  while(It.hasnext ()) {AAAA P=(AAAA) it.next (); System.out.print (p.getbasemenu_id ()+ "," +p.getbasemenu_name () + "," +p.getbasemenu_order () + "\ n"); }     }    //Single Object Public StaticObject Json2object (String Strjson, @SuppressWarnings ("Rawtypes") Class beanclass) {returnJsonobject.tobean (Jsonobject.fromobject (Strjson), beanclass); }     //Returns a list object array string json to add [] Public StaticObject json2objectlist (String Strjson, @SuppressWarnings ("Rawtypes") Class beanclass) {returnjsonarray.tocollection (Jsonarray.fromobject (Strjson), beanclass); }  

JSON to Java object and list collection

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.