'in java convert json object where parameter name is a value to a java opject or jpa entity [duplicate]

basically taking in a json object via a post api thats like the following

{
    [
        param_name: "name", 
        param_value: "jason"
        param_type: "varchar"
    ],
    [ 
        param_name: "age", 
        param_value: "15"
        param_type: "varchar"
    ]
}

then i want to convert it to a predefined java object where the parameter names are the same as the "param_value" values.

Is there anyway to do this in java, with a method like this?

student.setparamname(object.get("param_name"),object.get("param_value") );


Solution 1:[1]

You would want something like this if my interpretation of your question is correct. This allows you to POST to the endpoint with an object and specify name and age in json format. I'm going to say that the above is a Student for example but that's just an example object. If you want to include validation have a look at using @Valid. What you've declared in the question is a JsonArray (indicated by the [] after the {}) within a JsonObject but there is no need for that, just create the JsonObject with the fields you require.

@JsonDeserialize(builder = NewStudent.Builder.class)
    public class NewStudent{
    private String name;
    private String age;
    //getters and setters here
    //create builder here
    }

   public class Student{
private String name;
private String age;
//getters and setters here
}


@PostMapping(value = "/addstudent", produces = APPLICATION_JSON_VALUE, consumes = APPLICATION_JSON_VALUE)
public ResponseEntity<Student> createStudent(@RequestBody NewStudent newStudent){
Student student = new Student();
student.setName(newStudent.getName());
student.setAge(newStudent.getAge());
ResponseEntity<Student> responseContent = ResponseEntity.body(student);
return responseContent;

}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1