0

I have some problem fetching info from JSON. I'm confused about whether to use ArrayList or any other data type to retrieve data from JSON server.

I've tried to fetch data using

ArrayList<String>

in model.

Below is data format of JSON

[
   {
    "sun_timing": "{\"sun_from\":\"12:30\",\"sun_to\":\"4:30\"}",
    "mon_timing": "{\"mon_from\":\"3:00\",\"mon_to\":\"4:30\"}"

   },
   {
    "sun_timing": "{\"sun_from\":\"12:30\",\"sun_to\":\"4:30\"}",
    "mon_timing": "{\"mon_from\":\"3:00\",\"mon_to\":\"4:30\"}"

   }
]

I want to fetch all sun_timing data and mon_timing data.

That is sun_from,sun_to and mon_from,mon_to data.

Gulshan Yadav
  • 424
  • 5
  • 13

2 Answers2

0

try this out working for me

 private List<String> getSunList() {

    ArrayList sunList = new ArrayList<String>()
    String sun_json = your_json_string
    try {
        JSONObject jsonObject = new JSONObject(sun_json)
        Log.d(TAG, "jsonObject: "+jsonObject)
        Log.d(TAG, "jsonObject: "+sun_json)
        JSONArray jsonArray = jsonObject.getJSONArray("sun_timing")
        for (i in 0 until jsonArray.length())
        {
            JSONObject obj = jsonArray.get(i) as JSONObject
            String sun_from = obj.getString("sun_from")
            String sun_to = obj.getString("sun_to")
            sunList.add(sun_from)
            Log.d(TAG, "obj= "+obj)

        }

    }
    catch (e: java.lang.Exception)
    {

    }

    return sunList
}
HussainAbbas
  • 242
  • 2
  • 13
0

Your Plain Old Java Object(POJO) for your json looks like this:

public class Example {

    @SerializedName("sun_timing")
    @Expose
    private String sunTiming;

    @SerializedName("mon_timing")
    @Expose
    private String monTiming;

    public String getSunTiming() {
        return sunTiming;
    }

    public void setSunTiming(String sunTiming) {
        this.sunTiming = sunTiming;
    }

    public String getMonTiming() {
        return monTiming;
    }

    public void setMonTiming(String monTiming) {
        this.monTiming = monTiming;
    }
}

See also: https://stackoverflow.com/a/40973753/10452701 for more details about How to get json via Rerofit2.

Nurbol
  • 371
  • 2
  • 8
  • Why do you use both SerializedName and Expose? It's a common unnecessary thing while working with Retrofit. To simplifier, if you're happy with json property name, use just Expose, and if want to change use SerializedName – Said Jul 04 '19 at 06:26
  • how to call that POJO from activity to get data? – Gulshan Yadav Jul 04 '19 at 06:38
  • Bro see this answer https://stackoverflow.com/a/40973753/10452701. There is a detailed explanation, getting java object from Json response using Retrofit2. Use retrofit as explained there, @GulshanYadav – Nurbol Jul 04 '19 at 07:29
  • i better know how to get object from json. but don't know how to get nested object. tell me how to get nested objects if you know. – Gulshan Yadav Jul 04 '19 at 07:33
  • Use [this](http://www.jsonschema2pojo.org) to convert any Json to POJO (even nested objects). Than use needed object with retrofit2. It converts even nested objects @GulshanYadav – Nurbol Jul 04 '19 at 07:45
  • I use `SerializedName` for simplisity bro @Said – Nurbol Jul 04 '19 at 07:49
  • SerializedName anotation decides if you have the same property as response. Expose anotation decides if property will be serialized and deserialized, by default expose is Expose(serialize = true, deserialize = true). So, no need for both. – Said Jul 05 '19 at 06:22