JSON into Java objects

Source: Internet
Author: User

Avro generated code, string is charsequence, cannot be deserialized by Gson, so with the following code, Parsearray is not perfect:

1 Static<T> list<t> Parsearray (Jsonarray arrary,class<?> CLS)throwsillegalaccessexception, InvocationTargetException, Nosuchmethodexception, Instantiationexception, classnotfoundexception{2list<t> result =NewArraylist<t>();3String ClassName =cls.getname ();4        for(intI=0;i<arrary.length (); i++){ 5           if(Classname.contains ("Java.lang")){          6               if(Classname.equals ("Java.lang.CharSequence") | |7Classname.equals ("Java.lang.String")) {8 Result.add ((T) arrary.getstring (i));9}Else  if(Classname.equals ("Java.lang.Double")) {Ten Result.add ((T) ((Double) arrary.getdouble (i))); One}Else  if(Classname.equals ("Java.lang.Integer")) { A Result.add ((T) ((Integer) arrary.getint (i))); -}Else  if(Classname.equals ("Java.lang.Boolean")) { - Result.add ((T) ((Boolean) Arrary.getboolean (i))); the               }   -}Else{ -               //Parsing Objects - Result.add ((T) Json2bean (Arrary.getjsonobject (i), CLS)); +           }      -       }   +       returnresult; A   } at  -    Public Static<T> T Json2bean (jsonobject jsonobject, class<?> CLS)throwsIllegalaccessexception, - invocationtargetexception, Nosuchmethodexception, Instantiationexception, classnotfoundexception { - //if (item = = NULL) { - //return null;  - //        }   inT item =(T) cls.newinstance (); -field[] Fields =Cls.getdeclaredfields ();  to          for(Field field:fields) { +String VarName =Field.getname ();  -           if(Jsonobject.has (varName)) { theObject value =Jsonobject.get (varName); *  $Class<?> Currentclass =Field.gettype ();Panax Notoginseng              if(Currentclass.equals (List.class)){ -Jsonarray array =(Jsonarray) value; theString subclassname = Field.getgenerictype (). toString (). Replace ("java.util.list<", "" "); +Subclassname = subclassname.substring (0,subclassname.length ()-1); A //System.out.println (subclassname);  theClass<?> Clasz =Class.forName (subclassname); + //System.out.println (Z.getclass ()); - beanutils.setproperty (item, VarName, Parsearray (array, Clasz)); $              $}Else{ -                  if(Valueinstanceofjsonobject) { - beanutils.setproperty (item, VarName, Json2bean ((jsonobject) value,currentclass));  the}Else{ -                     if(Valueinstanceofjsonnull) {WuyiValue =NULL; the                     } - beanutils.setproperty (item, varName, value);  Wu                  } -              } About}Else{ $               //Set Default values -               //beanutils.setproperty (item, varName, NULL);  -           } -         }   A          returnitem;  +}

JSON into Java objects

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.