【问题标题】:Got half of data from JSON response从 JSON 响应中获得了一半的数据
【发布时间】:2020-04-04 14:44:25
【问题描述】:

我有来自改造的 JSON 响应:

    {
  "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"
}

我想在 Map 中存储 rate 对象的所有数据,这很奇怪,但我没有 rate 的所有数据。这就是我得到的。

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

这就像一半的 JSON 数据,那里发生了什么?

这里有一些代码:

API

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

主活动

    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());
            }
        });
    }
}

我该如何处理?我可以有任何提示吗? **抱歉代码乱码^^

【问题讨论】:

    标签: java android json retrofit


    【解决方案1】:

    您的问题在于部分代码:

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

    如您所见,在循环的每次迭代中,next() 被调用了两次,导致只有一半的元素被放入映射中。

    你应该做的是,只调用一次next(),但将结果保存在一些临时变量中。然后,使用该临时变量提取您需要的值。

    这应该可行:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-05-22
      • 2015-07-02
      • 1970-01-01
      • 1970-01-01
      • 2016-12-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多