1

I have a string [{Label_0=Mobile, Phone_0=+212131231}], stored in ArrayList within HashMap, I need to assign it to another Arraylist to fetch values but getting a syntax error

List<HashMap<String, String>> phonesArr = new ArrayList<HashMap<String, String>>();
HashMap<String, String> phoneDict = new HashMap<String, String>();
 phoneDict.put(String.format("Label_%d", locali1),PTYPE);
 phoneDict.put(String.format("Phone_%d", locali1),phoneNumber);
 phonesArr.add(phoneDict);

There is a reason why I am storing it in a array because phone Numbers can be multiple for one contacts.

 ArrayList<HashMap<String, String>> _allPeoplesDictArr = new ArrayList<HashMap<String, String>>();
 HashMap<String, String> personDict = new HashMap<String, String>();
 personDict.put("PhoneNumbers", phonesArr.toString()) //// Value stored as array to string
 // [{Label_0=Mobile, Phone_0=+212131231}]
 _allPeoplesDictArr.add(personDict);
 for (int j = 0; j < _allPeoplesDictArr.size(); j++) {
 String localPhoneStr = _allPeoplesDictArr.get(j)
 .get("PhoneNumbers");
 Log.d("localPhoneStr", localPhoneStr); /// I dont want to retrieve it as string 
 List<HashMap<String, String>> localPhoneArr = _allPeoplesDictArr.get(j).get("PhoneNumbers"); //// I want to retrieve it as array but getting syntax error.
 if (localPhoneArr.size() > 0) {
 for (int k = 0; k < localPhoneArr.size(); k++) {
 HashMap<String, String> userDict = new HashMap<String, String>();
 userDict.put(
 "label",
 localPhoneArr.get(k).get(
 String.format("Label_%d", k)));
 userDict.put(
 "value",
 localPhoneArr.get(k).get(
 String.format("Phone_%d", k)));
 }
 }
 }
 I am getting syntax error at grabing values that is now as a JSON `[{Label_0=Mobile, Phone_0=+212131231}]`
 List<HashMap<String, String>> localPhoneArr = _allPeoplesDictArr.get(j).get("PhoneNumbers");
asked Mar 11, 2014 at 15:17
4
  • 2
    It would be much easier to help you if you'd show a short but complete example demonstrating the problem. We don't know the type of _allPeoplesDictArr at the moment, for example. Commented Mar 11, 2014 at 15:24
  • 1
    Is _allPeoplesDictArr now a List<String> or a List<HashMap<String, String>>? Because you have String localPhoneStr = _allPeoplesDictArr.get(j).get("PhoneNumbers"); and List<HashMap<String, String>> localPhoneArr = _allPeoplesDictArr.get(j).get("PhoneNumbers"); Commented Mar 11, 2014 at 15:25
  • I have updated my question for better understanding _allPeoplesDictArr.get(j).get("PhoneNumbers"); this contains arraylist with Hash but I am unable to retrives its values. Commented Mar 12, 2014 at 5:16
  • @super-qua I know it is HashMap<String, String> instead of HashMap<String, List<HashMap<String, String>>>, but I isnt there any way to rectify my problem with the current assignment? Commented Mar 12, 2014 at 8:20

1 Answer 1

1

The line List<HashMap<String, String>> localPhoneArr = _allPeoplesDictArr.get(j).get("PhoneNumbers"); is giving you the syntax error, because it is a String. If you want to make an Array out of it, you have to parse the JSON String. You can find the documentation here

It should look something like this

String localPhoneStr = _allPeoplesDictArr.get(j).get("PhoneNumbers");
JSONArray phoneArray = new JSONArray(localPhoneStr);
for(int i=0; i<phoneArray.length(); i++){
 JSONObject phone = phoneArray.getJSONObject(i);
 userDict.put("label", phone.getString(String.format("Label_%d", i)));
 userDict.put("value", phone.getString(String.format("Phone_%d", i)));
}
answered Mar 12, 2014 at 8:56
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks Mate for the logic as I like it , I will try and hoping that it will work as per my needs.
A small modification required as userDict.put("label", phone.getString(String.format("Label_%d", i))); userDict.put("value", phone.getString(String.format("Phone_%d", i)));
Glad it helped you. I updated the answer to include your modifications for others to see them

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.