gpt4 book ai didi

java - 从 JSON 响应中获取一半数据

转载 作者:行者123 更新时间:2023-12-02 08:46:45 27 4
gpt4 key购买 nike

我有来自改造的 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 中,这很奇怪,但我没有费率的所有数据。这就是我得到的。

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

主要 Activity

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

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

最佳答案

您的问题出在代码的以下部分:

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

关于java - 从 JSON 响应中获取一半数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61029803/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com