Java 提取列表<;字符串>;要匹配LinkedHashMap的数据<;整数,列表<;字符串>&燃气轮机;
我有一个Java 提取列表<;字符串>;要匹配LinkedHashMap的数据<;整数,列表<;字符串>&燃气轮机;,java,linkedhashmap,Java,Linkedhashmap,我有一个列表数据,如下所示: {"0":["passFrom","3/9/2018","3/9/2018","anotherMethod","but"],"1":["googleForAlongTIme","3/9/2018","3/9/2018","stillCannotConvert","theLinkHashMap"]} 我需要用上面的数据存储到LinkedHashMap,到目前为止,我已经尝试了下面的方法 ArrayList<String> listdata = new A
列表
数据,如下所示:
{"0":["passFrom","3/9/2018","3/9/2018","anotherMethod","but"],"1":["googleForAlongTIme","3/9/2018","3/9/2018","stillCannotConvert","theLinkHashMap"]}
我需要用上面的数据存储到LinkedHashMap
,到目前为止,我已经尝试了下面的方法
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);
}*/
}
}
ArrayList listdata=new ArrayList();
Map listMap=新建LinkedHashMap();
if(jsonArray.getString(0.trim()!=null&&!jsonArray.getString(0.isEmpty()){
对于(inti=0;i
这里需要一些提示和帮助,因为我的最终目标是获取0,1
整数及以下数据以存储在listMap中
“passFrom”、“2018年3月9日”、“2018年3月9日”、“anotherMethod”、“but”
“GoogleForLongtime”、“2018年3月9日”、“2018年3月9日”、“仍然无法转换”、“链接哈希图”
试试这个:
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);
}
}
}
}
因此,您有一些类似于JSON的
字符串
s。。。也许可以使用JSON解析器来解析它们,然后您可以(更容易)访问这些属性。甚至可以将它们解析为POJO,这将使整个过程更易于处理。Yeap,我尝试使用json解析器,但服务器端不允许添加json标识符。我别无选择,只能在客户端说出这样的话。哈哈,谢谢。这个,但是在列表的每个元素的循环中。太棒了!我很高兴你高兴。祝你好运
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]