La mitad recibió de los datos de respuesta JSON

qwesd:

Tengo la respuesta JSON de adaptación que es:

    {
  "rates": {
    "CAD": 1.5299,
    "HKD": 8.3625,
    "ISK": 155.7,
    "PHP": 54.805,
    "DKK": 7.4689,
    "HUF": 365.15,
    "CZK": 27.539,
    "AUD": 1.8004,
    "RON": 4.8307,
    "SEK": 10.952,
    "IDR": 17918.68,
    "INR": 82.216,
    "BRL": 5.6893,
    "RUB": 82.8075,
    "HRK": 7.63,
    "JPY": 117.1,
    "THB": 35.601,
    "CHF": 1.0547,
    "SGD": 1.5489,
    "PLN": 4.5765,
    "BGN": 1.9558,
    "TRY": 7.2296,
    "CNY": 7.6476,
    "NOK": 11.2628,
    "NZD": 1.8423,
    "ZAR": 20.2642,
    "USD": 1.0785,
    "MXN": 26.547,
    "ILS": 3.9267,
    "GBP": 0.8785,
    "KRW": 1332.82,
    "MYR": 4.7006
  },
  "base": "EUR",
  "date": "2020-04-03"
}

Quería para almacenar todos los datos de las tasas objeto de mapa, y esto es raro, pero no tengo todos los datos de tasas. Esto es lo que me pasa.

I/System.out: ISK 54.805
I/System.out: HRK 117.1
I/System.out: DKK 365.15
I/System.out: CAD 8.3625
I/System.out: USD 26.547
I/System.out: BGN 7.2296
I/System.out: THB 1.0547
I/System.out: CNY 11.2628
I/System.out: RON 10.952
I/System.out: SGD 4.5765
I/System.out: ILS 0.8785
I/System.out: KRW 4.7006
I/System.out: CZK 1.8004
I/System.out: IDR 82.216
I/System.out: NZD 20.2642
I/System.out: BRL 82.8075

Es como la mitad de los datos JSON, lo que pasó por ahí?

Aquí hay algo de código:

api

    public interface ApiCall  {
    @GET("latest")
    Call<JsonObject> getTest();
}

MainActivity

    public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";
    JsonObject jsonObject;
    JSONObject asd;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        test();
    }

    public void test() {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("https://api.exchangeratesapi.io/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        ApiCall call = retrofit.create(ApiCall.class);
        call.getTest().enqueue(new Callback<JsonObject>() {
            @Override
            public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {
                if (!response.isSuccessful()) {
                    Log.d(TAG, "onResponse: error " + response.code());
                }

                Map<String, Double> TEST = new HashMap<>();

                //////////////////////////
                jsonObject = response.body().getAsJsonObject();
                try {
                    asd = new JSONObject(String.valueOf(jsonObject));
                } catch (JSONException e) {
                    e.printStackTrace();
                }

//                JSONObject plop = response.body().getJSONObject("rates");
                try {
                    JSONObject rates = asd.getJSONObject("rates");
                    Iterator<String> cur = rates.keys();
                    while (cur.hasNext()){
                        TEST.put(cur.next(), rates.getDouble(cur.next()));
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                for (Map.Entry<String, Double> entry : TEST.entrySet()) {
                    String key = entry.getKey();
                    Object value = entry.getValue();

                    System.out.println(key+" "+value+"\n");
                }
            }
            @Override
            public void onFailure(Call<JsonObject> call, Throwable t) {
                Log.d(TAG, "onFailure: " + t.getMessage());
            }
        });
    }
}

¿Cómo puedo lidiar con ella? ¿Puedo tener alguna pista? ** Lo siento por código desordenado ^^

radovix:

Su problema es parte siguiente del código:

while (cur.hasNext()){
  TEST.put(cur.next(), rates.getDouble(cur.next()));
}

Como se puede ver, en cada iteración del bucle, next()se llama dos veces, causando únicos elementos y medio para poner en el mapa.

Lo que debe hacer es, llamar next()solamente una vez, pero guardar el resultado en una variable temporal. A continuación, extraer los valores que necesita el uso de esa variable temporal.

Esto debería funcionar:

while (cur.hasNext()){
  String key = cur.next();

  TEST.put(key, rates.getDouble(key));
}

Supongo que te gusta

Origin http://10.200.1.11:23101/article/api/json?id=403988&siteId=1
Recomendado
Clasificación