2

Hello everyone I'm kind of new in this field. I made a rest request for project and I got this response:

{"success":true,"timestamp":1524649444,"base":"EUR","date":"2018-04-25","rates":{"AED":4.486623,"AFN":85.583411,"ALL":128.283655,"AMD":585.986726,"ANG":2.175126,"AOA":270.568194,"ARS":24.745299,"AUD":1.609775,"AWG":2.1 // and so on.

how can I insert the currency and his value to arraylist?

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_fixer_request);
    System.out.println("-->rest");

    String url = "http://data.fixer.io/api/latest?access_key=7e4976fbeb5d633e337487fea31fd7ca";
    RequestQueue requestQueue= Volley.newRequestQueue(this);

    JsonObjectRequest objectRequest= new JsonObjectRequest(
            Request.Method.GET,
            url,
            null,
            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    Log.e("rest Response",response.toString());
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Log.e("rest Response",error.toString());
                }
            }
    );

    requestQueue.add(objectRequest);
}
James
  • 2,756
  • 17
  • 19
Afik Menashe
  • 77
  • 1
  • 1
  • 8
  • please do some research before posting your query. this is a very basic thing. do refer here - http://aideafactory.com/json-serialization-and-deserialization-with-gson/. if you have any problem after this you can ask. – Sagar Nayak Apr 25 '18 at 10:44

5 Answers5

1

You have to change your JSON response like this:

{"success":true,"timestamp":1524649444,"base":"EUR","date":"2018-04-25","rates":[{"currency_name":"AED","currency_value":"4.486623"},{"currency_name":"AFN","currency_value":"85.583411"},{"currency_name":"ALL","currency_value":"128.283655"},{"currency_name":"AMD","currency_value":"585.986726"}]}

In your JSON there is no array to from which you can add value in array list.

If you can't change your JSON response than you have to pre-declare all type of currency in your array list and than add there currency accordingly.

Manoj Patidar
  • 302
  • 3
  • 17
0

Use Gson library to convert json object to Java object easily google-gson

Parinda Rajapaksha
  • 2,963
  • 1
  • 36
  • 40
0

Create a JSONObject:

JSONObject jObject = new JSONObject(result);

To get a specific string

String aJsonString = jObject.getString("STRINGNAME");

To get a specific boolean

boolean aJsonBoolean = jObject.getBoolean("BOOLEANNAME");

To get a specific integer

int aJsonInteger = jObject.getInt("INTEGERNAME");

To get a specific long

long aJsonLong = jObject.getBoolean("LONGNAME");

To get a specific double

double aJsonDouble = jObject.getDouble("DOUBLENAME");

To get a specific JSONArray:

JSONArray jArray = jObject.getJSONArray("ARRAYNAME");

To get the items from the array

for (int i=0; i < jArray.length(); i++)
{
    try {
        JSONObject oneObject = jArray.getJSONObject(i);
        // Pulling items from the array
        String oneObjectsItem = oneObject.getString("STRINGNAMEinTHEarray");
        String oneObjectsItem2 = oneObject.getString("anotherSTRINGNAMEINtheARRAY");
    } catch (JSONException e) {
        // Oops
    }
}
0

you can from http://www.jsonschema2pojo.org/ to convert json to java class annotation base are clear code

and you can use from library retrofit Gson base are easy

or use :

JSONArray jArray2 = jArray.getJSONArray("PreFactorHotels");

            for (int i = 0; i < jArray2.length(); i++) {
                hotelPreFactorModels.add(new HotelPreFactorModel(jArray2.getJSONObject(i).getString("HotelNameE"),
                        Utility.dateShow(jArray2.getJSONObject(i).getString("HotelChekin"))
                        , Utility.dateShow(jArray2.getJSONObject(i).getString("HotelChekout")),
                        jArray2.getJSONObject(i).getString("AdlCount"),
                        jArray2.getJSONObject(i).getString("ChdCount"),jArray2.getJSONObject(i).getString("RoomTitleFa")));

            }
0

USING GSON libray it requires three steps

String afikJson  = "YOUR JSON...";
 Gson gson = new Gson();//create Gson object
//and create Type object with arrayList as a type of data
Type afikListType = new TypeToken<ArrayList<Afik>>(){}.getType();

//map your JSON
List<Afik> afikList = gson.fromJson(afikJson, afikListType); 
Aelaf
  • 118
  • 1
  • 11