英文:
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:
"{\"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\"}"
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("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;
}
}
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<String, Double> 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("rates");// 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...
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论