Reputation: 883
In a Spring MVC controller method, I have a JSON input (request body) with just one field: {"version":3}
. I can get this value like this:
@RequestMapping
public void myMethod(@RequestBody MyJsonRequest myJsonRequest) {
int version = myJsonRequest.version;
//...
}
But I have to create MyJsonRequest
class with a version
field. How can I get the version
value without creating a new class?
Upvotes: 0
Views: 1852
Reputation: 8057
You can try using a map:
public void myMethod(@RequestBody Map<String, Integer> myJsonRequest) {
int version = myJsonRequest.get("version");
//...
}
Upvotes: 3
Reputation: 70
Try the code below, it maps your json object to a primitive int variable:
@RequestMapping(value="/yourMapping", method=RequestMethod.POST, consumes=MediaType.APPLICATION_JSON)
public void myMethod (@RequestBody int version) {
//do something with your int variable version
}
Upvotes: 0
Reputation: 1081
You can parse JSON data and then you can directly access to value of JSON response.
Consider data as myJsonRequest :
var version=0;
var jsonval = $.parseJSON(data);
// you can use directly value of json data like
version = data.version;
// If you returned an array in json response, then you can use $.each method and store value into single array, like this
$.each(portals_array,function(i,v){
version[] = v.version;
});
alert(version);
};
It's working perfectly in my code. Check it out.
Upvotes: 0