如何将Java自定义对象列表转换为JSON数组?

时间:2015-01-16 15:38:32

标签: ajax json spring-mvc

我正在开发Spring MVC应用程序。我需要使用AJAX获取JSON中的LocationModel类对象列表。以下是我的LocationModel类:

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;

@Entity
@Table(name="Location")
public class LocationModel {

    @Id
    @Column(name="locationid")
    @GeneratedValue 
    private int locationId;

    @Column(name="locationname")
    private String locationName;

    @Column(name="locationdesc")
    private String locationDescription;

    @Column(name="type")
    private String locationType;

    @Column(name="address")
    private String address;

    @Column(name="city")
    private String city;

    @Column(name="state")
    private String state;

    @Column(name="district")
    private String district;

    @Column(name="lattitude")
    private String lattitude;

    @Column(name="longitude")
    private String longitude;

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getLattitude() {
        return lattitude;
    }

    public void setLattitude(String lattitude) {
        this.lattitude = lattitude;
    }

    public String getLongitude() {
        return longitude;
    }

    public void setLongitude(String longitude) {
        this.longitude = longitude;
    }

    public String getLocationType() {
        return locationType;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }

    public String getDistrict() {
        return district;
    }

    public void setDistrict(String district) {
        this.district = district;
    }

    public void setLocationType(String locationType) {
        this.locationType = locationType;
    }

    public int getLocationId() {
        return locationId;
    }

    public void setLocationId(int locationId) {
        this.locationId = locationId;
    }

    public String getLocationName() {
        return locationName;
    }

    public void setLocationName(String locationName) {
        this.locationName = locationName;
    }

    public String getLocationDescription() {
        return locationDescription;
    }

    public void setLocationDescription(String locationDescription) {
        this.locationDescription = locationDescription;
    }
}

下面是jsp页面中的ajax块。

function locationList(){

    var locationName = jQuery('#locationName').val();
    var json = {"name" : locationName};  
    jQuery.ajax({
        url: '<c:url value="/contact/ajax" />',
        type: 'POST',
        data: JSON.stringify(json),
        cache:false,
        beforeSend: function(xhr) {  
            xhr.setRequestHeader("Accept", "application/json");  
            xhr.setRequestHeader("Content-Type", "application/json");
        },
        success:function(response){
            alert("Validation: "+response.validation+"   Name: "+response.name+"  Location: "+response.location);
        },
        error:function(jqXhr, textStatus, errorThrown){
            alert(textStatus);
        }
    });
    return true;
}

我们如何从AJAX向控制器传递数据?如何在json和parse中获取LocationModel对象的列表?我搜索了很多地方,但找不到答案。

2 个答案:

答案 0 :(得分:0)

  

JSONObject是一个无序的名称/值对集合。它的   外部表单是一个用大括号括起来的字符串   名称和值,以及值和名称之间的逗号。该   内部表单是一个具有get()和opt()方法的对象   按名称访问值,使用put()方法添加或   按名称替换值。值可以是以下任何类型:   Boolean,JSONArray,JSONObject,Number和String,或者   JSONObject.NULL对象。

JSONObject Documentation

public JSONObject() // Construct an empty JSONObject.

您还可以查看Json-lib

答案 1 :(得分:0)

您可以使用Spring Data Rest,看看:

http://projects.spring.io/spring-data-rest/