繁体   English   中英

提取清单 <String> 数据以匹配LinkedHashMap <Integer, List<String> &gt;

[英]Extract List<String> data to Match LinkedHashMap<Integer, List<String>>

我有一个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”,“另一个方法”,“但是”“ googleForAlongTIme”,“ 3/9/2018”,“ 3/9/2018”,“ stillCannotConvert” “theLinkHashMap”

尝试这个:

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]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM