[英]JSON String to JSON Object in java
Here is my JSON String 这是我的JSON字符串
"[{"name":"3214657890RootSAPSSE","children":[{"name":"672BENIAMEEN .Sales Base Market SectionCustomer Representative"},{"name":"672BENIAMEEN .Sales Base Market SectionPeão"}]}]"
When I parse it to json object in java I get null. 当我将其解析为java中的json对象时,我会得到null。 This Field is coming from HTML hidden field where it is coming from java script. 此字段来自HTML隐藏字段,而该字段来自Java脚本。
Here is my java code 这是我的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());
Here is my Java Class 这是我的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;
}
}
Don't double-parse the string like that; 不要像这样对字符串进行双重解析; it doesn't make sense to do so. 这样做是没有意义的。 Pass x
directly to GSON. 将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());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.