I have an implementation where when user click on checkbox , a json gets associated as value of checkbox and that value is passed to my bean class. And In the method invoked, the String is then parsed into JSON object. When I select two checkbox, it works perfectly fine. But if I select one checkbox, then it gives me error.
Here is my Checkbox Bean class -
private ArrayList<String> Ancillary=new ArrayList<String>() ;
public ArrayList<String> getAncillary() {
for(int i=0;i<Ancillary.size();i++){
System.out.println(i+"Object:" +Ancillary.get(i)+"\n\n\n");
}
return Ancillary;
}
public void setAncillary(ArrayList<String> ancillary) {
Ancillary = ancillary;
}
Here is my method where I print value of a Particular key in JSON.
public Event updatePax(RequestContext context) throws Exception {
ExtrasMenu extrasMenu = (ExtrasMenu) context.getConversationScope().get(ScopeKeys.EXTRASMENU);
System.out.println("As a string:"+extrasMenu.getAncillary().toString());
JSONObject json=new JSONObject(extrasMenu.getAncillary().get(0));
System.out.println(json.get("firstName"));
}
And here is the Output-
If only one checkbox is selected -
0Object:{"firstName":"TIMOTHY"
1Object:"lastName":"WALKER"
2Object:"price":100}
If two or more checkboxes are selected -
0Object:{"firstName":"TIMOTHY","lastname":"WALKER","price":"50"}
1Object:{"firstName":"ANNE","lastname":"WALKER","price":"150"}