A client is sending me a JSON file through HTTP PUT, here is the file :
{
"nomPers": "Testworking",
"prenomPers": "WorkingTest",
"loginPers": "Work",
"pwdPers": "Ing",
"active": true
},
I'm using HTTPServlet as WebService framework and the org.json library to work with Json. I'm also using Tomcat Server. As Tomcat can't create a parameter map for this HTTP verb, i've to work with JSON objects.
So I did some searching and tries but still can't make it work, here is my code :
@Override
public void doPut(HttpServletRequest request, HttpServletResponse response) {
// this parses the incoming json to a json object.
JSONObject jObj = new JSONObject(request.getParameter("jsondata"));
Iterator<String> it = jObj.keys();
while(it.hasNext())
{
String key = it.next(); // get key
Object o = jObj.get(key); // get value
System.out.println(key + " : " + o); // print the key and value
}
So i'm parsing the incoming Json to a Json object to work with, then I create an Iterator to be able to loop through this object and get and and print datas for each key/value pair.
The problem is I get a NullPointerException error.
I guess it's because of the request.getParameter("jsondata"). It seems I don't get any parameters. I guess i've to create a string from the datas i get through the request to feed the JSONObject constructor, but i don't get how to achieve this.