I want to change the string into LinkedList,I write the code but it happen in to a exception,java.util.ArrayList cannot be cast to java.util.LinkedList
,How to fix it?
LinkedList<String> names=new LinkedList<String>();
names.add("a");
names.add("b");
String str=JSON.toJSONString(names);
System.out.println(str);
System.out.println((LinkedList<String>)JSON.parseArray(str,String.class));
You can't cast an
ArrayList
to aLinkedList
so you have to create aLinkedList
. Trynew LinkedList<String>(JSON.parseArray(str, String.class))
. This creates a LinkedList containing all the elements of the JSON array. If the list does not have to be exactly aLinkedList
, you can also try just using theList
interface.