Convert a JSON String to a JSON Object
<p>I am making the following PUT request using AJAX to a RESTful web service. Before making the request, I convert the data returned from form submission (using ng-submit) to a JSON String:</p>

<pre><code>var serializedData =angular.toJson(person);
</code></pre>

<p>I then send it as data in an AJAX call like this:</p>

<pre><code>    $http({
        method: 'PUT',
        url: myurl +person.personID,
        data: serializedData,
        headers: {
            'Content-Type': 'application/json'
        }
    }).success(function(data) {

        console.log("Success");

    });
</code></pre>

<p>Now in my web service, I want to get this data and create a Java object:</p>

<pre><code>@PUT
    @Path("/person/{id}")
    @Produces({ MediaType.APPLICATION_JSON }) 
    @Consumes({MediaType.APPLICATION_JSON}) 
public Response updatePerson(@PathParam("id") String personID,
        @Context HttpServletRequest httpRequest) {


    Response.ResponseBuilder builder = Response.ok();
    Person person =null;
    String data =null;
    BufferedReader reader =null;
    StringBuffer buffer = new StringBuffer();
    try
    {
        reader = new BufferedReader(new InputStreamReader(httpRequest.getInputStream()));
    }
    catch (IOException e1)
    {
        e1.printStackTrace();
        e1.getCause().toString();
    }

    try
    {
        while((data =reader.readLine()) != null){
            buffer.append(data);
        }
    }
    catch (IOException e1)
    {
        e1.printStackTrace();
    }
    String bufferToString =buffer.toString();


    try
    {
        person =JSONUtil.toObject(bufferToString, new TypeReference&lt;Person&gt;() {});
    }
    catch (JsonParseException e1)
    {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    catch (JsonMappingException e1)
    {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    catch (IOException e1)
    {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    if (person == null) {
        builder = Response
                .status(Response.Status.NOT_FOUND.getStatusCode());
        return builder.build();
    }

    try {

        deviceService.update(dev);
        JSONUtil.toJSONString(dev);
        builder.entity(JSONUtil.toJSONString(dev));
    } catch (JsonGenerationException e) {
    } catch (JsonMappingException e) {
    } catch (IOException e) {

    return builder.build();  
}
</code></pre>

<p>Here is a problem I cant seem to figure out. When the ajax call is made, data does reach to the web service, which means it is a JSON string because that is what <code>angular.toJson</code> does [<a href="https://docs.angularjs.org/api/ng/function/angular.toJson]" rel="nofollow">https://docs.angularjs.org/api/ng/function/angular.toJson]</a>. After reading it with the reader and converting it to a string and passing it to the <code>JSONUtil.toObject(bufferToString, new TypeReference&lt;Person&gt;() {});</code>, it gives me a 404 status code, meaning the person object is null. Now here is the weird thing I dont understand. If I do a get request for a Person object in POSTMAN client, edit a value in that JSON and send it as body of application/json type in a PUT request (from POSTMAN), then it works. I have checked through JSONLint that the JSON string that I receive from AJAX is valid.. Why is this happening?? I passed a string to the <code>JSONUtil.toObject(bufferToString, new TypeReference&lt;Person&gt;() {});</code>, which is what that method needs. Is it not in the right format? I cant seem to understand. Any help will be appreciated. Have been stuck on this for 4 hours now. Thank you.</p>