-2

I'm trying to get nested json object using retrofit. here is my json result

 {
  "results":
  {
    "sunrise":"7:27:02 AM",
    "sunset":"5:05:55 PM",
    "solar_noon":"12:16:28 PM",
    "day_length":"9:38:53",
    "civil_twilight_begin":"6:58:14 AM",
    "civil_twilight_end":"5:34:43 PM",
    "nautical_twilight_begin":"6:25:47 AM",
    "nautical_twilight_end":"6:07:10 PM",
    "astronomical_twilight_begin":"5:54:14 AM",
    "astronomical_twilight_end":"6:38:43 PM"
  },
   "status":"OK"
}

But I don't need all fields and only need sunrise field. How I get that sunrise field without other fields.

Surya Prakash Kushawah
  • 3,185
  • 1
  • 22
  • 42
  • use this to generate model class and remove unnecessary fields: http://www.jsonschema2pojo.org/ – Divyesh Patel Dec 12 '16 at 10:18
  • You have to make another POJO class for results and add the object using serializable name of "results" – Zahidul Islam Dec 12 '16 at 10:18
  • Duplicate Question found: see http://stackoverflow.com/questions/14898768/how-to-access-nested-elements-of-json-object-using-getjsonarray-method and http://stackoverflow.com/questions/20899839/retreiving-values-from-nested-json-object – saeid rastak Dec 12 '16 at 10:20

1 Answers1

2
JSONObject object = new JSONObject((String) o);
String sunrise = object.getJSONObject("results").getString("sunrise");

Use this.Hope help you.

Deepak Sachdeva
  • 950
  • 8
  • 16
Moeen Kashisaz
  • 302
  • 1
  • 3
  • 13