I am new to Java and stick to this problem. I am creating an array in JavaScript that looks like this:
var jsonObj = [];
jsonObj.push(
{
Effect: "Deny",
RuleID: "Rule1"
},
{
Effect: "Deny",
RuleID: "Rule2"
},
{
Effect: "Deny",
RuleID: "Rule3"
},....
)
After that, I pass this to the servlet using Ajax:
jQuery.ajax({
url: "http://localhost:8080/PolicyConsumerServlet/PolicyServlet",
dataType: "json",
type: 'POST',
data: {jsondata : JSON.stringify(jsonObj)},
contentType: 'application/json',
mimeType: 'application/json',
success: function(data) {
alert('Hi');
},
error: function(jqXHR, textStatus, errorThrown) {
alert("error occurred");
}
});
In the servlet, in the doPostmethod, I wrote the code below
StringBuffer jb = new StringBuffer();
String line = null;
BufferedReader reader = request.getReader();
RequestMaker.requestProcess();
while ((line = reader.readLine()) != null)
jb.append(line);
String jsonstring = jb.toString();
Gson gson = new Gson();
Wrapper[] data = gson.fromJson(jsonstring, Wrapper[].class);
System.out.println(jb);
and class Wrapper
public class Wrapper {
String Effect;
String RuleID;
}
But this throws an exception below the line
Wrapper [] data = gson.fromJson(jsonstring, Wrapper[].class);
What is wrong with parsing this JSON?