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

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

getting values from a Gson object into a HashMap

问题

我有以下的 JSON 字符串:

  1. "{
  2. \"rates\":{
  3. \"CAD\":1.5601,
  4. \"HKD\":8.4781,
  5. \"ISK\":156.1,
  6. \"PHP\":55.709,
  7. \"DKK\":7.4642,
  8. \"HUF\":369.36,
  9. \"CZK\":27.369,
  10. \"AUD\":1.8053,
  11. \"RON\":4.834,
  12. \"SEK\":10.9368,
  13. \"IDR\":18239.61,
  14. \"INR\":83.6004,
  15. \"BRL\":5.7349,
  16. \"RUB\":86.475,
  17. \"HRK\":7.6285,
  18. \"JPY\":117.55,
  19. \"THB\":36.111,
  20. \"CHF\":1.0564,
  21. \"SGD\":1.5689,
  22. \"PLN\":4.5815,
  23. \"BGN\":1.9558,
  24. \"TRY\":7.2925,
  25. \"CNY\":7.7653,
  26. \"NOK\":11.2685,
  27. \"NZD\":1.8547,
  28. \"ZAR\":19.6619,
  29. \"USD\":1.0936,
  30. \"MXN\":26.4097,
  31. \"ILS\":3.9015,
  32. \"GBP\":0.8846,
  33. \"KRW\":1346.48,
  34. \"MYR\":4.7654
  35. },
  36. \"base\":\"EUR\",
  37. \"date\":\"2020-04-01\"
  38. }"

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

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

  1. package com.example.android.myrates.core.json;
  2. import com.google.gson.annotations.Expose;
  3. import com.google.gson.annotations.SerializedName;
  4. public class JsonLatestStructure {
  5. @SerializedName("rates")
  6. @Expose
  7. private JsonRatesStructure rates;
  8. @SerializedName("base")
  9. @Expose
  10. private String base;
  11. @SerializedName("date")
  12. @Expose
  13. private String date;
  14. public JsonRatesStructure getRates() {
  15. return rates;
  16. }
  17. public void setRates(JsonRatesStructure rates) {
  18. this.rates = rates;
  19. }
  20. public String getBase() {
  21. return base;
  22. }
  23. public void setBase(String base) {
  24. this.base = base;
  25. }
  26. public String getDate() {
  27. return date;
  28. }
  29. public void setDate(String date) {
  30. this.date = date;
  31. }
  32. }

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

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

提前感谢您的帮助!

更新:

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

  1. JsonLatestStructure jsonData = new Gson().fromJson(rateListPresenter.getLatestRateList(), JsonLatestStructure.class);
  2. Map<String, Double> rateListItems = jsonData.getRates();
英文:

I have the following json String:

  1. &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

  1. package com.example.android.myrates.core.json;
  2. import com.google.gson.annotations.Expose;
  3. import com.google.gson.annotations.SerializedName;
  4. public class JsonLatestStructure {
  5. @SerializedName(&quot;rates&quot;)
  6. @Expose
  7. private JsonRatesStructure rates;
  8. @SerializedName(&quot;base&quot;)
  9. @Expose
  10. private String base;
  11. @SerializedName(&quot;date&quot;)
  12. @Expose
  13. private String date;
  14. public JsonRatesStructure getRates() {
  15. return rates;
  16. }
  17. public void setRates(JsonRatesStructure rates) {
  18. this.rates = rates;
  19. }
  20. public String getBase() {
  21. return base;
  22. }
  23. public void setBase(String base) {
  24. this.base = base;
  25. }
  26. public String getDate() {
  27. return date;
  28. }
  29. public void setDate(String date) {
  30. this.date = date;
  31. }
  32. }

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.

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

Thanks in advance!

UPDATE:

I accomplished what I wanted by doing this:

  1. JsonLatestStructure jsonData = new Gson().fromJson(rateListPresenter.getLatestRateList(), JsonLatestStructure.class);
  2. Map&lt;String, Double&gt; rateListItems = jsonData.getRates();

答案1

得分: 0

  1. 尝试以下代码它将返回所有动态键和值列表
  2. JSONObject data = jsonResponse.getJSONObject("rates"); // 这里的 response 是服务器响应
  3. Iterator keys = data.keys();
  4. while(keys.hasNext()) {
  5. // 循环以获取动态键
  6. String key = (String) keys.next(); // 它返回一个键,如 CAD、HKD 等...
  7. // 获取动态键的值
  8. int value = data.getInt(key); // 它返回一个值,如 1.5601、8.4781 等...
  9. }
英文:

Try below it will return all dynamic key and value list

  1. JSONObject data = jsonResponse.getJSONObject(&quot;rates&quot;);// here response is server response
  2. Iterator keys = data.keys();
  3. while(keys.hasNext()) {
  4. // loop to get the dynamic key
  5. String key = (String)keys.next(); // it returns a key ;ile CAD, HKD etc...
  6. // get the value of the dynamic key
  7. int value = data.getInt(key); // it returns a value like 1.5601,8.4781 etc...
  8. }

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:

确定