string到带有hashmap的arraylist

jpfvwuh4  于 2021-07-09  发布在  Java
关注(0)|答案(1)|浏览(308)

我有一根绳子 [{Label_0=Mobile, Phone_0=+212131231}] ,存储在 ArrayListHashMap ,我需要分配给另一个 Arraylist 获取值但获取语法错误

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

我之所以要将其存储在一个数组中,是因为一个联系人可以有多个电话号码。

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

jm2pwxwz1#

线路 List<HashMap<String, String>> localPhoneArr = _allPeoplesDictArr.get(j).get("PhoneNumbers"); 是给你语法错误,因为它是一个 String . 如果你想做一个 Array 除此之外,还必须解析json字符串。你可以在这里找到文档
应该是这样的

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

相关问题