java - Serializing JSON with varing datatype with the same variable -


i trying out opensignal api , have run wall when trying use jackson 2.0 serialize result. networkrank can array or can string

how set jackson serialize json properly, if it's string, save string networkrank attribute else save list <networkrank> = new arraylist<networkrank>()

these result of api: (check out networkrank below)

{     "apiversion": "2",     "latitude": "14.55669",     "longitude": "121.370119",     "distance": "10",     "network_type": "10",     "perminutecurrent": 0,     "perminutelimit": 10,     "permonthcurrent": 7,     "permonthlimit": 2000,     "networkrank": "no results area"    } 

and

{     "apiversion": "2",     "latitude": "14.55669",     "longitude": "121.370119",     "distance": "10",     "network_type": "10",     "perminutecurrent": 0,     "perminutelimit": 10,     "permonthcurrent": 7,     "permonthlimit": 2000,     "networkrank": [          {          "networkname": "t-mobile",          "networkid": "310260",   "type3g": {     "networkname": "t-mobile",     "networkid": "310260",     "networktype": "3",     "averagerssiasu": "10.934899",     "averagerssidb": "-91.130203",     "samplesizerssi": "437532",     "downloadspeed": "4532.8305",     "uploadspeed": "1124.9649",     "pingtime": "148.8205",     "reliability": "94.5944170771741"   },   "type4g": {     "networkname": "ee",     "networkid": "23430",     "networktype": "4",     "averagersrpasu": "33.865632",     "averagersrpdb": "-106.134368",     "samplesizersrp": "284041",     "downloadspeed": "9851.1000",     "uploadspeed": "4377.3200",     "pingtime": "87.0000",     "reliability": "96.2113095238094"      }    }   ]   } 

the simplest way use same field either json string or array use object field. solution not need custom serializer , deserializer. following example class jackson annotation:

@jsonautodetect(fieldvisibility = jsonautodetect.visibility.any) public class networkstatistics {      private string apiversion;     private string latitude;     private string longitude;     private string distance;     @jsonproperty("network_type")     private string networktype;     private int perminutecurrent;     private int perminutelimit;     private int permonthcurrent;     private int permonthlimit;     private object networkrank;      public networkstatistics() {     }      // getters } 

following example deserialize json string of api response networkstatistics instance:

objectmapper mapper = new objectmapper(); networkstatistics networkstatistics = mapper.readvalue(json, networkstatistics.class); 

and example serialize networkstatistics instance json string:

string json = mapper.writevalueasstring(networkstatistics)) 

if networkrank in json string value, instance field after deserialization string field. if json array, instance field java.util.arraylist field.

hope can help.


Comments

Popular posts from this blog

sql - invalid in the select list because it is not contained in either an aggregate function -

Angularjs unit testing - ng-disabled not working when adding text to textarea -

How to start daemon on android by adb -