Get nested JSON object with GSON using retrofit

前端 未结 13 749
挽巷
挽巷 2020-11-22 17:04

I\'m consuming an API from my android app, and all the JSON responses are like this:

{
    \'status\': \'OK\',
    \'reason\': \'Everything was fine\',
    \         


        
相关标签:
13条回答
  • 2020-11-22 17:46

    In my case, the "content" key would change for each response. Example:

    // Root is hotel
    {
      status : "ok",
      statusCode : 200,
      hotels : [{
        name : "Taj Palace",
        location : {
          lat : 12
          lng : 77
        }
    
      }, {
        name : "Plaza", 
        location : {
          lat : 12
          lng : 77
        }
      }]
    }
    
    //Root is city
    
    {
      status : "ok",
      statusCode : 200,
      city : {
        name : "Vegas",
        location : {
          lat : 12
          lng : 77
        }
    }
    

    In such cases I used a similar solution as listed above but had to tweak it. You can see the gist here. It's a little too large to post it here on SOF.

    The annotation @InnerKey("content") is used and the rest of the code is to facilitate it's usage with Gson.

    0 讨论(0)
提交回复
热议问题