2015-01-16 31 views
0

我正在處理Spring MVC應用程序。我需要使用AJAX獲取JSON中的LocationModel類對象列表。以下是我的LocationModel類:如何將Java自定義對象列表轉換爲JSON數組?

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和解析中的LocationModel對象列表?我搜查了很多地方,但無法找到答案。

回答

0

JSONObject是名稱/值對的無序集合。其 外部形式是一個用大括號括起來的字符串,其中包含名稱和值之間的冒號,以及值和名稱之間的逗號。 內部表單是一個對象,它具有get()和opt()方法,用於 按名稱訪問值,put()方法用於添加或按名稱替換值。這些值可以是以下任何類型: 布爾值,JSONArray,JSONObject,數字和字符串,或者 JSONObject.NULL對象。

JSONObject Documentation

public JSONObject() // Construct an empty JSONObject.

您還可以檢查出Json-lib

相關問題