0

I have to get bitcoin money for every week. And if I use JsonObject, then I get the json, BUT I CAN NOT USE THE CYCLE to take all the data. And if I use JSONObject, then retured null. Please,Help me!

This is my api:

https://api.coindesk.com/v1/bpi/historical/close.json?start=2018-08-01&end=2018-08-19

{"bpi":{"2018-08-01":7603.7488,"2018-08-02":7535.02,"2018-08-03":7415.5613,"2018-08-04":7009.0888,"2018-08-05":7026.9913,"2018-08-06":6937.0738,"2018-08-07":6717.2088,"2018-08-08":6280.58,"2018-08-09":6537.9025,"2018-08-10":6143.305,"2018-08-11":6233.3813,"2018-08-12":6312.8338,"2018-08-13":6252.37,"2018-08-14":6192.3063,"2018-08-15":6270.0425,"2018-08-16":6314.2413,"2018-08-17":6583.2388,"2018-08-18":6395.3525},"disclaimer":"This data was produced from the CoinDesk Bitcoin Price Index. BPI value data returned as USD.","time":{"updated":"Aug 19, 2018 13:06:45 UTC","updatedISO":"2018-08-19T13:06:45+00:00"}}
1
  • Review the answer below, have verified my self working as expected. Commented Aug 19, 2018 at 14:25

2 Answers 2

0

Use Gson, simply add below code in your model class.

@SerializedName("bpi")
JsonElement results;

public Map<String, String> getResults() {
    Type type = new TypeToken<Map<String, String>>() {}.getType();

    final Map<String, String> objects = new Gson().fromJson(results, type);
    return objects;
}

Here is how your model class will look like

public class Model {
    @SerializedName("disclaimer")
    private String disclaimer;

    @SerializedName("time")
    private Time time;

    @SerializedName("bpi")
    JsonElement results;

    public Map<String, String> getResults() {
        Type type = new TypeToken<Map<String, String>>() {}.getType();
        final Map<String, String> objects = new Gson().fromJson(results, type);
        return objects;
    }

    public String getDisclaimer() {
        return disclaimer;
    }

    public void setDisclaimer(String disclaimer) {
        this.disclaimer = disclaimer;
    }

    public Time getTime() {
        return time;
    }

    public void setTime(Time time) {
        this.time = time;
    }

    public class Time {

        @SerializedName("updated")
        private String updated;

        @SerializedName("updatedISO")
        private String updatedISO;

        public String getUpdated() {
            return updated;
        }

        public void setUpdated(String updated) {
            this.updated = updated;
        }

        public String getUpdatedISO() {
            return updatedISO;
        }

        public void setUpdatedISO(String updatedISO) {
            this.updatedISO = updatedISO;
        }
    }
}

Here is your API interface method

@GET("/v1/bpi/historical/close.json")
Call<Model> getData(@Query("start") String start,@Query("end") String end);
Sign up to request clarification or add additional context in comments.

Comments

0

You can use like below to parse data

 JSONObject sys  = reader.getJSONObject("bpi");
 String country = sys.getString("2018-08-01");

If it is dynamic then you may use like below

JSONObject mainJSONObj=new JSONObject(<json_string>);
// get category JSONObject from mainJSONObj
JSONObject categoryJSONObj=mainJSONObj.getJSONObject("category");
// get all keys from categoryJSONObj
Iterator<String> iterator = categoryJSONObj.keys();
  while (iterator.hasNext()) {
    String key = iterator.next();
    Log.i("TAG","key:"+key +"--Value::"+categoryJSONObj.optString(key);
  }

Anyway you can try Retrofit & Gson to parse and make it model and easy parsing.

2 Comments

If I use JSONObject it return null data. But If I use JsonObject, its worked.
Your JsonObject is from com.google.gson.JsonObject and JSONObject is from org.json.JSONObject

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.