Here is my JSON String
"[{"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. This Field is coming from HTML hidden field where it is coming from java script.
Here is my java code
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
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.
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());
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.