String to ArrayList with HashMAP

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

Answers


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

Need Your Help

Splitting game into multiple standalone parts

flash flash-builder game-engine starling-framework citrus-engine

I'm digging into Starling's CitrusEngine game engine. I try to understand the idea of structuring the game.