提取List <string>数据以匹配LinkedHashMap <integer,list <string =“” >>

时间:2018-07-16 23:32:44

标签: java linkedhashmap

我有一个List<String>数据,例如:

{"0":["passFrom","3/9/2018","3/9/2018","anotherMethod","but"],"1":["googleForAlongTIme","3/9/2018","3/9/2018","stillCannotConvert","theLinkHashMap"]}

我需要将上面的数据存储到linkeHashMap中,到目前为止,我已经尝试过以下操作。

ArrayList<String> listdata = new ArrayList<String>();
Map<Integer, List<String>> listMap = new LinkedHashMap<Integer, List<String>>();

if (jsonArray.getString(0).trim()!= null && !jsonArray.getString(0).isEmpty()) {
    for (int i = 0; i < jsonArray.length(); i++){ 
        listdata.add(jsonArray.getString(i)); // here is the data which shown above
        //trying to use split at here but find out `**["passFrom","3/9/2018","3/9/2018","anotherMethod","but"],"1"**` is not the correct data

        /*List<String> bothList= Arrays.asList(listdata.get(i).toString().split(":"));
        for (String string : bothList) {
             List<String> tempData=Arrays.asList(bothList.toString());
             listMap.put(i, tempData);
             System.out.println("TeST: " + string);
         }*/
    }
}

这里需要一些提示和帮助,因为我的最终目标是获取0,1整数及以下数据以存储在listMap中

  

“ passFrom”,“ 3/9/2018”,“ 3/9/2018”,“ anotherMethod”,“ but”   “ googleForAlongTIme”,“ 3/9/2018”,“ 3/9/2018”,“ stillCannotConvert”,“ theLinkHashMap”

1 个答案:

答案 0 :(得分:1)

尝试一下:

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;
import java.util.List;
import java.util.Map;

public class ParseJson {

    public static void main(String[] args) throws IOException {

        ObjectMapper objectMapper = new ObjectMapper();

        final String jsonStr = "{\"0\":[\"passFrom\",\"3/9/2018\",\"3/9/2018\",\"anotherMethod\",\"but\"],\"1\":[\"googleForAlongTIme\",\"3/9/2018\",\"3/9/2018\",\"stillCannotConvert\",\"theLinkHashMap\"]}";

        Map<Integer, List<String>> map = objectMapper.readValue(jsonStr, new TypeReference<LinkedHashMap<Integer, List<String>>>(){});

        for (Map.Entry<Integer, List<String>> entry : map.entrySet()) {
            System.out.printf("For item \"%d\", values are:\n", entry.getKey());
            for (String value : entry.getValue()) {
                System.out.printf("\t[%s]\n", value);
            }
        }
    }
}

输出:

For item "0", values are:
    [passFrom]
    [3/9/2018]
    [3/9/2018]
    [anotherMethod]
    [but]
For item "1", values are:
    [googleForAlongTIme]
    [3/9/2018]
    [3/9/2018]
    [stillCannotConvert]
    [theLinkHashMap]