Converting JSON data to Java object

后端 未结 12 1110
春和景丽
春和景丽 2020-11-21 05:11

I want to be able to access properties from a JSON string within my Java action method. The string is available by simply saying myJsonString = object.getJson()

12条回答
  •  执念已碎
    2020-11-21 05:43

    Easy and working java code to convert JSONObject to Java Object

    Employee.java

    import java.util.HashMap;
    import java.util.Map;
    
    import javax.annotation.Generated;
    
    import com.fasterxml.jackson.annotation.JsonAnyGetter;
    import com.fasterxml.jackson.annotation.JsonAnySetter;
    import com.fasterxml.jackson.annotation.JsonIgnore;
    import com.fasterxml.jackson.annotation.JsonInclude;
    import com.fasterxml.jackson.annotation.JsonProperty;
    import com.fasterxml.jackson.annotation.JsonPropertyOrder;
    
    @JsonInclude(JsonInclude.Include.NON_NULL)
    @Generated("org.jsonschema2pojo")
    @JsonPropertyOrder({
    "id",
    "firstName",
    "lastName"
    })
    public class Employee {
    
    @JsonProperty("id")
    private Integer id;
    @JsonProperty("firstName")
    private String firstName;
    @JsonProperty("lastName")
    private String lastName;
    @JsonIgnore
    private Map additionalProperties = new HashMap();
    
    /**
    *
    * @return
    * The id
    */
    @JsonProperty("id")
    public Integer getId() {
    return id;
    }
    
    /**
    *
    * @param id
    * The id
    */
    @JsonProperty("id")
    public void setId(Integer id) {
    this.id = id;
    }
    
    /**
    *
    * @return
    * The firstName
    */
    @JsonProperty("firstName")
    public String getFirstName() {
    return firstName;
    }
    
    /**
    *
    * @param firstName
    * The firstName
    */
    @JsonProperty("firstName")
    public void setFirstName(String firstName) {
    this.firstName = firstName;
    }
    
    /**
    *
    * @return
    * The lastName
    */
    @JsonProperty("lastName")
    public String getLastName() {
    return lastName;
    }
    
    /**
    *
    * @param lastName
    * The lastName
    */
    @JsonProperty("lastName")
    public void setLastName(String lastName) {
    this.lastName = lastName;
    }
    
    @JsonAnyGetter
    public Map getAdditionalProperties() {
    return this.additionalProperties;
    }
    
    @JsonAnySetter
    public void setAdditionalProperty(String name, Object value) {
    this.additionalProperties.put(name, value);
    }
    
    }
    

    LoadFromJSON.java

    import org.codehaus.jettison.json.JSONObject;
    
    import com.fasterxml.jackson.databind.ObjectMapper;
    
    public class LoadFromJSON {
    
        public static void main(String args[]) throws Exception {
            JSONObject json = new JSONObject();
            json.put("id", 2);
            json.put("firstName", "hello");
            json.put("lastName", "world");
    
            byte[] jsonData = json.toString().getBytes();
    
            ObjectMapper mapper = new ObjectMapper();
            Employee employee = mapper.readValue(jsonData, Employee.class);
    
            System.out.print(employee.getLastName());
    
        }
    }
    

提交回复
热议问题