在android中使用Json对象文件创建数组列表

时间:2012-03-12 03:57:03

标签: java android

请查看我的以下代码

String jsonString = writer.toString();
        JSONObject jsonObj = new JSONObject(jsonString); 
        defaultCurrencyValue = jsonObj.getString(DefaultCurrencyKey);
        currenciesTypes = jsonObj.get(CurrenciesKey);

当我使用Debugger

时,这就是我获得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) 

请有人告诉我如何将key及其values保存在两个数组列表中?

最好的问候

3 个答案:

答案 0 :(得分:2)

@ user966227:

嘿,请参考此链接,它肯定会帮助你....

http://www.vogella.de/articles/AndroidJSON/article.html

在此页面中显示了将数据从JSON文件获取到数组的教程。但我认为你可以用它来转换成数组列表...

答案 1 :(得分:2)

让两个arraylists说出键和值:

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));
        }

答案 2 :(得分:2)

    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);
    }