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.