Deserializing json from retrofit using jackson where same variable name can represent two different objects

后端 未结 4 1600
有刺的猬
有刺的猬 2021-01-21 09:46

The response from retrofit2 may be of the following types.(and we don\'t know before hand which response will come)

{
    \"id\": \"abc\",
    \"place\": \"LA\",         


        
4条回答
  •  日久生厌
    2021-01-21 10:20

    You can check whether the json has values inside it;

    String jsonString= "{ ... }";
    Object json = new JSONTokener(jsonString).nextValue();
     if (json instanceof JSONObject){ 
       //do operations related with object
     }
    else if (json instanceof JSONArray) {
     //do operations based on an array
    }
    

提交回复
热议问题