这是我的JSON字符串
"[{"name":"3214657890RootSAPSSE","children":[{"name":"672BENIAMEEN .Sales Base Market SectionCustomer Representative"},{"name":"672BENIAMEEN .Sales Base Market SectionPeão"}]}]"
当我将它解析为java中的json对象时,我得到null。该字段来自HTML隐藏字段,它来自java脚本。
这是我的java代码
String x = request.getParameter("JSONString");
System.out.println(x);
JSONArray jsonObject = (JSONArray) JSONValue.parse(x);
Gson gson = new Gson();
java.lang.reflect.Type type = new TypeToken<List<EmployeeJSONObj>>(){}.getType();
List<EmployeeJSONObj>l = gson.fromJson(jsonObject.toJSONString(), type);
System.out.println(l.get(0).getName());
这是我的Java类
public class EmployeeJSONObj {
private String name;
private List<EmployeeJSONObj> children = new LinkedList<>();
EmployeeJSONObj()
{
}
public String getName()
{
return name;
}
public String toString() {
return "name: " + name + ", children = " + children;
}
}
答案 0 :(得分:0)
不要像这样双重解析字符串;这样做是没有意义的。将x
直接传递给GSON。
String x = request.getParameter("JSONString");
Gson gson = new Gson();
java.lang.reflect.Type type = new TypeToken<List<EmployeeJSONObj>>(){}.getType();
List<EmployeeJSONObj>l = gson.fromJson(x, type);
System.out.println(l.get(0).getName());