英文:
Got half of data from JSON response
问题
以下是从Retrofit获得的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"
}
我想要将rates对象的所有数据存储在Map中,但奇怪的是,我没有rates的所有数据。这是我得到的数据。
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();
}
MainActivity类:
public class MainActivity extends AppCompatActivity {
// ... 其他代码 ...
public void test() {
// ... 其他代码 ...
call.getTest().enqueue(new Callback<JsonObject>() {
@Override
public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {
// ... 其他代码 ...
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();
}
// ... 其他代码 ...
}
// ... 其他代码 ...
});
}
}
我该如何处理这个问题?你能给我一些提示吗?**对于代码的混乱感到抱歉 ^^
英文:
I have JSON response from retrofit which is:
{
"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"
}
I wanted to store all data of rates object in Map, and this is weird, but I don't have all the data from rates. This is what I get.
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
It's like half of the JSON data, what happened out there?
Here's some code:
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());
}
});
}
}
How do I deal with it? Can I have any hints?
**Sorry for messy code ^^
答案1
得分: 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));
}
英文:
Your problem is following part of the code:
while (cur.hasNext()){
TEST.put(cur.next(), rates.getDouble(cur.next()));
}
As you can see, in each iteration of the loop, next()
is called twice, causing only half elements to be put in the map.
What you should do is, call next()
only once, but save result in some temporary variable. Then, extract the values that you need using that temporary variable.
This should work:
while (cur.hasNext()){
String key = cur.next();
TEST.put(key, rates.getDouble(key));
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论