从 JSON 响应中获取了一半的数据。

huangapple go评论99阅读模式
英文:

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:

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

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(&quot;latest&quot;)
    Call&lt;JsonObject&gt; getTest();
}

MainActivity

    public class MainActivity extends AppCompatActivity {
    private static final String TAG = &quot;MainActivity&quot;;
    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(&quot;https://api.exchangeratesapi.io/&quot;)
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        ApiCall call = retrofit.create(ApiCall.class);
        call.getTest().enqueue(new Callback&lt;JsonObject&gt;() {
            @Override
            public void onResponse(Call&lt;JsonObject&gt; call, Response&lt;JsonObject&gt; response) {
                if (!response.isSuccessful()) {
                    Log.d(TAG, &quot;onResponse: error &quot; + response.code());
                }
                
                Map&lt;String, Double&gt; TEST = new HashMap&lt;&gt;();

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

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

                    System.out.println(key+&quot; &quot;+value+&quot;\n&quot;);
                }
            }
            @Override
            public void onFailure(Call&lt;JsonObject&gt; call, Throwable t) {
                Log.d(TAG, &quot;onFailure: &quot; + 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));
}

huangapple
  • 本文由 发表于 2020年4月4日 22:44:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/61029803.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定