2

Please check out my following code

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

This is what i get the values of curenciesType object class variable when i used Debugger

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) 

Please anyone can tell me how can i save key and its values in two array lists?

Best Regards

asked Mar 12, 2012 at 3:57
1
  • why you need two different array lists..you can store it within one array list.. Commented Mar 12, 2012 at 4:11

3 Answers 3

2

Have two arraylists say keys, and values:

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));
 }
answered Mar 12, 2012 at 4:20
Sign up to request clarification or add additional context in comments.

1 Comment

Awesome solution. Just used it in combination with a HashMap. Thank you
2

@user966227 :

Hey, refer this link it will definitely help you out....

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

In this page tutorial of getting data from JSON file into array is shown. But i think you can use that for converting into array list also...

answered Mar 12, 2012 at 4:19

1 Comment

look i have a JSONOBJECT Class variable currenciesTypes , from it, i want to retrieve both key and value. I can get value if i use variable.get(Key). But here in this case, i want to store key too in an array
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);
 }
answered Mar 12, 2012 at 4:41

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.