Serialization and deserialization of JSON in Android

Source: Internet
Author: User

Recently, the project needs to convert the object into a JSON string and deserialization as an object in Android. After reading the android SDK documentation, it is found that there is no class similar to datacontractjsonserializer in Android C, if you think that the requirements are not complex, you can write two solutions on your own,CodeAs follows:

Public Static String getjson (Object objsource ){
Try {
Object object = getjsonobject (objsource );
If (Object = Null )
Return "";
Return (Jsonobject) object). tostring ();
} Catch (Exception ERR ){
Log. E (TAG, "getjson exception:" + err. getmessage ());
Return "";
}
}

Private Static Object getjsonobject (Object objsource) Throws Exception {
If (Objsource! = Null ){
Class <?> Objclass = objsource. getclass ();
If (Objclass. getpackage (). getname (). startswith (packagestring )){
Jsonobject = New Jsonobject ();
Field [] publicfields = objclass. getfields ();
Object val;
For (Field: publicfields ){
Val = field. Get (objsource );
If (Val! = Null ){
Class <?> Valclass = Val. getclass ();
If (Valclass. isarray ()){
Jsonarray array = New Jsonarray ();
Int Length = array. getlength (VAL );
For ( Int I = 0; I <length; I ++ ){
Array. Put (getjsonobject (array. Get (Val, I )));
}
Jsonobject. Put (field. getname (), array );
} Else If (Valclass. Equals (arraylist. Class )){
Arraylist <?> Alist = arraylist. Class . Cast (VAL );
Iterator <?> Iterator = alist. iterator ();
Jsonarray array = New Jsonarray ();
While (Iterator. hasnext ()){
Array. Put (getjsonobject (iterator. Next ()));
}
Jsonobject. Put (field. getname (), array );
} Else {
Jsonobject. Put (field. getname (), getjsonobject (VAL ));
}
} Else {
Jsonobject. Put (field. getname (), Null );
}
}
Return Jsonobject;
} Else {
Return Objsource;
}
}
Return Null ;
}


Public Static Object parsejson (class <?> Targetclass, string jsonstring ){
Try {
Object object = targetclass. newinstance ();
Jsonobject = New Jsonobject (jsonstring );
String key;
Class <?> Fieldclass;
OBJECT element;
Class <?> Elementtype;
Field [] fields = targetclass. getfields ();

For (Field: fields ){

Key = field. getname ();
Fieldclass = field. GetType ();
If (Fieldclass. Equals (arraylist. Class )){
String generictype = field. getgenerictype (). tostring ();
If (Generictype. indexof ("<")> 0 ){
Generictype = generictype. substring (generictype. indexof ("<") + 1, generictype. lastindexof ("> "));
}
Arraylist <Object> alist = New Arraylist <Object> ();
Jsonarray array = jsonobject. getjsonarray (key );
Int Length = array. Length ();
If (Generictype. startswith (packagestring )){
Elementtype = Class. forname (generictype );
Field [] elementfields = elementtype. getfields ();
Jsonobject subjsonobject;
For ( Int I = 0; I <length; I ++ ){
Element = elementtype. newinstance ();
Subjsonobject = array. getjsonobject (I );
For (Field subfield: elementfields ){
Subfield. Set (element, subjsonobject. Get (subfield. getname ()));
}
Alist. Add (element );
}
} Else {
For ( Int I = 0; I <length; I ++ ){
Alist. Add (array. Get (I ));
}
}
Field. Set (object, alist );

} Else If (Fieldclass. isarray ()){
Jsonarray array = jsonobject. getjsonarray (key );
Int Length = array. Length ();
String arraytypename = fieldclass. getcomponenttype (). getname ();
Object [] objarray = New Object [length];

If (Arraytypename. startswith (packagestring )){
Elementtype = Class. forname (arraytypename );
Field [] elementfields = elementtype. getfields ();
Jsonobject subjsonobject;
For ( Int I = 0; I <length; I ++ ){
Element = elementtype. newinstance ();
Subjsonobject = array. getjsonobject (I );
For (Field subfield: elementfields ){
Subfield. Set (element, subjsonobject. Get (subfield. getname ()));
}
Objarray [I] = element;
}
} Else {
For ( Int I = 0; I <length; I ++ ){
Objarray [I] = array. Get (I );
}
}
Field. Set (object, objarray );

} Else {
Field. Set (object, jsonobject. Get (key ));
}
}
Return Object;
} Catch (Exception ERR ){
Log. E (TAG, "parsejson exception:" + err. getmessage ());
Return Null ;
}
}

Classes composed of basic types, arrays, and arraylist members are supported.

Related Article

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.