Java 在android中使用Json对象文件创建数组列表
请检查我的以下代码Java 在android中使用Json对象文件创建数组列表,java,android,Java,Android,请检查我的以下代码 String jsonString = writer.toString(); JSONObject jsonObj = new JSONObject(jsonString); defaultCurrencyValue = jsonObj.getString(DefaultCurrencyKey); currenciesTypes = jsonObj.get(CurrenciesKey); 这是我使用调试器时得到的curenc
String jsonString = writer.toString();
JSONObject jsonObj = new JSONObject(jsonString);
defaultCurrencyValue = jsonObj.getString(DefaultCurrencyKey);
currenciesTypes = jsonObj.get(CurrenciesKey);
这是我使用调试器时得到的curenciesType对象类变量的值
currenciesTypes JSONObject (id=830084916104)
myHashMap HashMap (id=830084916120)
[0] HashMap$HashMapEntry (id=830084916440)
key "PKR" (id=830084916256)
value "Rs" (id=830084916368)
[1] HashMap$HashMapEntry (id=830084917208)
key "EUR" (id=830084917064)
value "€" (id=830084917176)
[2] HashMap$HashMapEntry (id=830084916696)
[3] HashMap$HashMapEntry (id=830084916952)
请任何人告诉我如何在两个数组列表中保存键
及其值
最诚挚的问候@user966227:
嘿,参考这个链接,它肯定会帮你解决问题
在本页教程中,我们将介绍如何将数据从JSON文件获取到数组中。但我认为您也可以使用它来转换为数组列表…有两个数组列表,表示键和值:
String jsonString = writer.toString();
JSONObject jsonObj = new JSONObject(jsonString);
currenciesTypes = jsonObj.get(CurrenciesKey);
ArrayList<String> keys=new ArrayList<String>();
ArrayList<String> values=new ArrayList<String>();
Iterator<String> iterator=currencyType.keys();
while(iterator.hasNext())
{
String key=iterator.next();
keys.add(key);
values.add(currencyType.get(key));
}
String jsonString=writer.toString();
JSONObject jsonObj=新的JSONObject(jsonString);
currenciesTypes=jsonObj.get(CurrenciesKey);
ArrayList键=新的ArrayList();
ArrayList值=新的ArrayList();
迭代器迭代器=currencyType.keys();
while(iterator.hasNext())
{
String key=iterator.next();
key.add(key);
add(currencyType.get(key));
}
String jsonString=writer.toString();
JSONObject jsonObj=新的JSONObject(jsonString);
JSONObject currenciesTypes=jsonObj.get(CurrenciesKey);
List keyValuePairList=新的ArrayList();
迭代器迭代器=currenciesTypes.keys();
while(iterator.hasNext())
{
String key=iterator.next();
Pair keyValue=新的Pair(key,(String)currencestypes.get(key));
keyValuePairList.add(keyValue);
}
为什么需要两个不同的数组列表..您可以将其存储在一个数组列表中..看,我有一个JSONOBJECT类变量currencyStypes,我想从中检索键和值。如果我使用variable.get(Key),我可以得到值。但在本例中,我也希望将密钥存储在ArrayWesome解决方案中。只是将它与HashMap结合使用。非常感谢。
String jsonString = writer.toString();
JSONObject jsonObj = new JSONObject(jsonString);
JSONObject currenciesTypes = jsonObj.get(CurrenciesKey);
List<Pair<String,String>> keyValuePairList=new ArrayList<Pair<String,String>>();
Iterator<String> iterator=currenciesTypes.keys();
while(iterator.hasNext())
{
String key=iterator.next();
Pair<String,String> keyValue=new Pair<String,String>(key, (String) currenciesTypes.get(key));
keyValuePairList.add(keyValue);
}