您不能将此json转换为,
List但可以将其转换为
Map。
看到你的json
String:
..."Example": [ { "foo": "a1", "bar": "b1", "fubar": "c1" }, { "foo": "a2", "bar": "b2", "fubar": "c2" }, ...]}在此,“ Example”是键(字符串),值是Example的List对象。
试试这个:
parser.addTypeHint("Example[]", Example.class); Map<String,List<Example>> result1 = parser.parse(Map.class, json); for (Entry<String, List<Example>> entry : result1.entrySet()) { for (Example example : entry.getValue()) { System.out.println("VALUE :->"+ example.getFoo()); } }的完整代码
Example:
import java.util.List;import java.util.Map;import java.util.Map.Entry;import org.svenson.JSONParser;public class Test { public static void main(String[] args) { JSonParser parser = new JSonParser(); parser.addTypeHint(".Example[]", Example.class); String json = "{" + ""Example": [" + "{" + ""foo": "a1"," + ""bar": "b1"," + ""fubar": "c1"" + "}," + "{" + ""foo": "a2"," + ""bar": "b2"," + ""fubar": "c2"" + "}," + "{" + ""foo": "a3"," + ""bar": "b3"," + ""fubar": "c3"" + "}" + "]" + "}""; parser.addTypeHint("Example[]", Example.class); Map<String, List<Example>> result1 = parser.parse(Map.class, json); for (Entry<String, List<Example>> entry : result1.entrySet()) { for (Example example : entry.getValue()) { System.out.println("VALUE :->" + example.getFoo()); } } }}public class Example { private String foo; private String bar; private String fubar; public Example(){} public void setFoo(String foo) { this.foo = foo; } public String getFoo() { return foo; } public void setBar(String bar) { this.bar = bar; } public String getBar() { return bar; } public void setFubar(String fubar) { this.fubar = fubar; } public String getFubar() { return fubar; }}输出 :
VALUE :->a1VALUE :->a2VALUE :->a3



