从Gson对象中获取值到HashMap中。

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

getting values from a Gson object into a HashMap

问题

我有以下的 JSON 字符串:

"{
   \"rates\":{
      \"CAD\":1.5601,
      \"HKD\":8.4781,
      \"ISK\":156.1,
      \"PHP\":55.709,
      \"DKK\":7.4642,
      \"HUF\":369.36,
      \"CZK\":27.369,
      \"AUD\":1.8053,
      \"RON\":4.834,
      \"SEK\":10.9368,
      \"IDR\":18239.61,
      \"INR\":83.6004,
      \"BRL\":5.7349,
      \"RUB\":86.475,
      \"HRK\":7.6285,
      \"JPY\":117.55,
      \"THB\":36.111,
      \"CHF\":1.0564,
      \"SGD\":1.5689,
      \"PLN\":4.5815,
      \"BGN\":1.9558,
      \"TRY\":7.2925,
      \"CNY\":7.7653,
      \"NOK\":11.2685,
      \"NZD\":1.8547,
      \"ZAR\":19.6619,
      \"USD\":1.0936,
      \"MXN\":26.4097,
      \"ILS\":3.9015,
      \"GBP\":0.8846,
      \"KRW\":1346.48,
      \"MYR\":4.7654
   },
   \"base\":\"EUR\",
   \"date\":\"2020-04-01\"
}"

这是一组带有匹配汇率值的列表,这些数据从 这里 获取。

我想要做的是将这个字符串反序列化为我已经创建的对象。

package com.example.android.myrates.core.json;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class JsonLatestStructure {

    @SerializedName("rates")
    @Expose
    private JsonRatesStructure rates;
    @SerializedName("base")
    @Expose
    private String base;
    @SerializedName("date")
    @Expose
    private String date;

    public JsonRatesStructure getRates() {
        return rates;
    }

    public void setRates(JsonRatesStructure rates) {
        this.rates = rates;
    }

    public String getBase() {
        return base;
    }

    public void setBase(String base) {
        this.base = base;
    }

    public String getDate() {
        return date;
    }

    public void setDate(String date) {
        this.date = date;
    }

}

我目前有一个方法创建了一个新的 Gson 对象,并且我希望能够将所有汇率名称和匹配的汇率值放入一个哈希映射中。

JsonLatestStructure jsonData = new Gson().fromJson(rateListPresenter.getLatestRateList(), JsonLatestStructure.class);

提前感谢您的帮助!

更新:

我通过以下方式实现了我想要的:

JsonLatestStructure jsonData = new Gson().fromJson(rateListPresenter.getLatestRateList(), JsonLatestStructure.class);

Map<String, Double> rateListItems = jsonData.getRates();
英文:

I have the following json String:

&quot;{\&quot;rates\&quot;:{\&quot;CAD\&quot;:1.5601,\&quot;HKD\&quot;:8.4781,\&quot;ISK\&quot;:156.1,\&quot;PHP\&quot;:55.709,\&quot;DKK\&quot;:7.4642,\&quot;HUF\&quot;:369.36,\&quot;CZK\&quot;:27.369,\&quot;AUD\&quot;:1.8053,\&quot;RON\&quot;:4.834,\&quot;SEK\&quot;:10.9368,\&quot;IDR\&quot;:18239.61,\&quot;INR\&quot;:83.6004,\&quot;BRL\&quot;:5.7349,\&quot;RUB\&quot;:86.475,\&quot;HRK\&quot;:7.6285,\&quot;JPY\&quot;:117.55,\&quot;THB\&quot;:36.111,\&quot;CHF\&quot;:1.0564,\&quot;SGD\&quot;:1.5689,\&quot;PLN\&quot;:4.5815,\&quot;BGN\&quot;:1.9558,\&quot;TRY\&quot;:7.2925,\&quot;CNY\&quot;:7.7653,\&quot;NOK\&quot;:11.2685,\&quot;NZD\&quot;:1.8547,\&quot;ZAR\&quot;:19.6619,\&quot;USD\&quot;:1.0936,\&quot;MXN\&quot;:26.4097,\&quot;ILS\&quot;:3.9015,\&quot;GBP\&quot;:0.8846,\&quot;KRW\&quot;:1346.48,\&quot;MYR\&quot;:4.7654},\&quot;base\&quot;:\&quot;EUR\&quot;,\&quot;date\&quot;:\&quot;2020-04-01\&quot;}&quot;

it's a list of rates with the matching rate values, these have been retrieved from here.

What I want to do, is to deserialize the string into an object, that I've already created

package com.example.android.myrates.core.json;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class JsonLatestStructure {

    @SerializedName(&quot;rates&quot;)
    @Expose
    private JsonRatesStructure rates;
    @SerializedName(&quot;base&quot;)
    @Expose
    private String base;
    @SerializedName(&quot;date&quot;)
    @Expose
    private String date;

    public JsonRatesStructure getRates() {
        return rates;
    }

    public void setRates(JsonRatesStructure rates) {
        this.rates = rates;
    }

    public String getBase() {
        return base;
    }

    public void setBase(String base) {
        this.base = base;
    }

    public String getDate() {
        return date;
    }

    public void setDate(String date) {
        this.date = date;
    }

}

I currently have a method that creates a new Gson object, and I want to be able to get all the rate names and matching rate values into a hashmap.

JsonLatestStructure jsonData = new Gson().fromJson(rateListPresenter.getLatestRateList(), JsonLatestStructure.class);

Thanks in advance!

UPDATE:

I accomplished what I wanted by doing this:

JsonLatestStructure jsonData = new Gson().fromJson(rateListPresenter.getLatestRateList(), JsonLatestStructure.class);

Map&lt;String, Double&gt; rateListItems = jsonData.getRates();

答案1

得分: 0

尝试以下代码它将返回所有动态键和值列表

JSONObject data = jsonResponse.getJSONObject("rates"); // 这里的 response 是服务器响应
Iterator keys = data.keys();

while(keys.hasNext()) {
    // 循环以获取动态键
    String key = (String) keys.next();  // 它返回一个键,如 CAD、HKD 等...

    // 获取动态键的值
    int value = data.getInt(key);      // 它返回一个值,如 1.5601、8.4781 等...
}
英文:

Try below it will return all dynamic key and value list

JSONObject data = jsonResponse.getJSONObject(&quot;rates&quot;);// here response is server response
    Iterator keys = data.keys();

    while(keys.hasNext()) {
        // loop to get the dynamic key
        String key = (String)keys.next();  // it returns a key ;ile CAD, HKD etc...

        // get the value of the dynamic key
        int value = data.getInt(key);      // it returns a value like 1.5601,8.4781 etc...

    }

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

发表评论

匿名网友

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

确定