K-V-mode conversion used when "Java" extracts JSON values when encountering array collections

Source: Internet
Author: User

1. Entity class Conversion method

Reference article: http://www.cnblogs.com/dflmg/p/6933811.html

2.k-v method (This method is rather stupid, but there is no way, I now do not know whether there is no relevant simple API, can only manually spell a method.) Import org.apache.commons.lang.StringUtils;)

String s = "{' request ': {' Enterpriseid ': ' Ownercode ': ' SunEee01 ', ' item ': ' {' itemname ': ' 1 bear children Xinjiang Figs finished 268g ', ' ItemType ' : ' ZC ', ' barCode ': ' 6924459400256 ', ' Shelflife ': 0, ' ItemCode ': ' xhzwhggcp_268 '},
{' ItemName ': ' 2 good kids Xinjiang Fig finished 268g ', ' itemType ': ' ZC ', ' barCode ': ' 6924459400256 ', ' shelflife ': 1, ' ItemCode ': ' Xhzwhggcp_ 268 '}]}} "; //String s = "{' request ': {' Enterpriseid ':", ' ownercode ': ' SunEee01 ', ' item ': [{' ItemName ': ' Good kid Xinjiang figs finished 268g ', ' ItemType ': ' ZC ', ' barCode ': ' 6924459400256 ', ' shelflife ': 1, ' ItemCode ': ' xhzwhggcp_268 '}]} ";Jsonobject Jsonobject =Jsonobject.fromobject (s); //System.out.println (jsonobject);String Request= jsonobject.getstring ("Request"); //System.out.println (request);Jsonobject Vendordo=Jsonobject.fromobject (Request); //System.out.println (Vendordo);String ent= Vendordo.getstring ("Item"); //Method of Object entity classSystem.out.println (ENT); /*Jsonarray Ents = Vendordo.getjsonarray ("item"); list<item> ss = (list<item>) jsonarray.tolist (Ents,item.class); for (Item x:ss) {
If the item also has a list object as a property, use the 1 method, see http://www.cnblogs.com/dflmg/p/6933811.html System.out.println (X.getitemname ()) ; }*/string[] Many= Ent.split ("\\},\\{"); if(many.length==1) {String one= Many[0].substring (1,ent.length ()-1); Jsonobject Joss=Jsonobject.fromobject (one); String ItemName= Joss.getstring ("ItemName"); System.out.println (ItemName); }Else{ for(inti=0;i<many.length;i++){ if(i==0) {String frist= many[i].substring (1) + "}"; Jsonobject Joss=Jsonobject.fromobject (frist); String ItemName= Joss.getstring ("ItemName"); System.out.println (ItemName); }Else if(i==many.length-1) {String middle= "{" +stringutils.substringbefore (Many[i], "]"); Jsonobject Joss=jsonobject.fromobject (middle); String ItemName= Joss.getstring ("ItemName"); System.out.println (ItemName); }Else{String last= "{" +many[i]+ "}"; Jsonobject Joss=Jsonobject.fromobject (last); String ItemName= Joss.getstring ("ItemName"); System.out.println (ItemName); } }}

Java extracts JSON values when you encounter an array collection using the K-v transform

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.