0

我需要将此对象列表转换为 JSON,以便将其传递给我的 AJAX。

到目前为止我有(使用java playframework)


应用程序.java

@Transactional
public static Result fetchOptions(String strCategoryId, String strCategoryName) {

    Main mainAppModel = new Main();
    mainAppModel.populateOptions(strCategoryId, strCategoryName);
    return ok(Json.toJson(mainAppModel));
  }


主.java

  public void populateCountryOptions() {
    Countries countriesDb = new Countries();
    lstCountries = countriesDb.getCountriesByRegion(strRegionId);
  }

  public void populateLocationOptions() {
    Locations locationsDb = new Locations();
    lstLocations = locationsDb.getLocationByCountry(strCountryId);
  }

  public void populateDepartmentOptions() {
    Departments departmentsDb = new Departments();
    lstDepartments = departmentsDb.getDepartmentByLocationId(strLocationId);
  }

  public void populateOptions(String strCategoryId, String strCategoryName) {
    switch (strCategoryName) {
      case "region":
        strRegionId = strCategoryId;
        populateCountryOptions();
        break;
      case "country":
        strCountryId = strCategoryId;
        populateLocationOptions();
        break;
      case "location":
        strLocationId = strCategoryId;
        populateDepartmentOptions();
        break;
      default:
        break;
    }
  }


配置/路由

POST    /fetchOptions/:strCategoryId/:strCategoryName   controllers.Application.fetchOptions(strCategoryId: String, strCategoryName: String)


Departments.java(删除了 getter 和 setter)

package models.db;

import java.util.ArrayList;
import java.util.List;

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

import play.db.jpa.JPA;

@Table(name = "DEPARTMENTS")
@Entity
public class Departments {

  @Id
  @Column(name = "DEPARTMENT_ID")
  private String strDepartmentId;

  @Column(name = "DEPARTMENT_NAME")
  private String strDepartmentName;

  @Column(name = "MANAGER_ID")
  private String strManagerId;

  @Column(name = "LOCATION_ID")
  private String strLocationId;


  @SuppressWarnings("unchecked")
  public List<Departments> getDepartmentByLocationId(String strLocationId) {

    StringBuilder sb = new StringBuilder();
    sb.append(" SELECT ");
    sb.append("         DEPARTMENT_ID ");
    sb.append("         , DEPARTMENT_NAME ");
    sb.append("         , MANAGER_ID ");
    sb.append("         , LOCATION_ID ");
    sb.append(" FROM  ");
    sb.append("         DEPARTMENTS ");
    sb.append(" WHERE ");
    sb.append("         LOCATION_ID = '" + strLocationId + "'");
    sb.append(" ORDER BY  ");
    sb.append("         DEPARTMENT_NAME ASC ");

    List<Departments> lstDepartments = new ArrayList<Departments>();
    try {
      lstDepartments = JPA.em().createNativeQuery(sb.toString(), Departments.class).getResultList();
    } catch (Exception e) {
      // TODO: handle exception
    }

    return lstDepartments;

  }
}


Country.java(用于比较)

package models.db;

import java.util.ArrayList;
import java.util.List;

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

import play.db.jpa.JPA;

@Table(name = "COUNTRIES")
@Entity
public class Countries {


  @Id
  @Column(name = "COUNTRY_ID")
  private String strCountryId;

  @Column(name = "COUNTRY_NAME")
  private String strCountryName;

  @Column(name = "REGION_ID")
  private String strRegionId;

  @SuppressWarnings("unchecked")
  public List<Countries> getCountriesByRegion(String strRegionId) {

    StringBuilder sb = new StringBuilder();
    sb.append(" SELECT ");
    sb.append("         COUNTRY_ID ");
    sb.append("         , COUNTRY_NAME ");
    sb.append("         , REGION_ID ");
    sb.append(" FROM  ");
    sb.append("         COUNTRIES ");
    sb.append(" WHERE ");
    sb.append("         REGION_ID = '" + strRegionId + "'");
    sb.append(" ORDER BY ");
    sb.append("         COUNTRY_NAME ASC ");

    List<Countries> lstCountries = new ArrayList<Countries>();
    try {
      lstCountries = JPA.em().createNativeQuery(sb.toString(), Countries.class).getResultList();
    } catch (Exception e) {
      // TODO: handle exception
    }

    return lstCountries;
  }


}


我认为不再需要 javascript,因为将mainAppModel对象返回为Json时发生错误。
所以我在Main.java上有 3 个方法,在Application.java上使用相同的方法。如果我使用populateCountryOptions()populateLocationOptions() Json 工作正常,但每当我使用populateDepartmentOptions()时就会发生无限递归。该模型看起来彼此相似。我不知道是什么导致了这个错误。谢谢你的帮助!

4

2 回答 2

0

你需要在这里休息:

case "location":
        strLocationId = strCategoryId;
        populateDepartmentOptions();
    break;
于 2017-08-17T10:10:40.203 回答
0

因为我无法修复错误。我通过覆盖.toString() 手动创建自己的 JSON,

尽管这不是我的问题的特定答案,但无论如何我都会发布这个以帮助其他可能遇到与.toJson()方法 相同问题的其他人,这是

我的代码:

主.java

添加

@Override
  public String toString() {
    return "{\"lstCountries\":" + lstCountries + ",\"lstLocations\":" + lstLocations
        + ",\"lstDepartments\":" + lstDepartments + "}";
  }


国家.java

@Override
  public String toString() {
    return "{\"strCountryId\":\"" + strCountryId + "\",\"strCountryName\":\"" + strCountryName
        + "\",\"strRegionId\":\"" + strRegionId + "\"}";
  }


位置.java

@Override
  public String toString() {
    return "{\"strLocationId\":\"" + strLocationId + "\",\"strStreetAddress\":\""
        + strStreetAddress + "\",\"strPostalCode\":\"" + strPostalCode + "\",\"strCity\":\""
        + strCity + "\",\"strStateProvince\":\"" + strStateProvince + "\",\"strCountryId\":\""
        + strCountryId + "\"}";
  }


部门.java

@Override
  public String toString() {
    return "{\"strDepartmentId\":\"" + strDepartmentId + "\",\"strDepartmentName\":\""
        + strDepartmentName + "\",\"strManagerId\":\"" + strManagerId + "\",\"strLocationId\":\""
        + strLocationId + "\"}";
  }


应用程序.java

然后我将返回更改为:

@Transactional
  public static Result fetchOptions(String strCategoryId, String strCategoryName) {

    Main mainAppModel = new Main();
    mainAppModel.populateOptions(strCategoryId, strCategoryName);
    return ok(Json.toJson(mainAppModel.toString()));
  }


javascript

这是获取国家列表的 ajax

$.ajax({
        type : 'POST',
        url : "/fetchOptions/" + region + "/region",
        dataType: "json",
        success : function(data) {
            data = JSON.parse(data);
            //some codes here..
            //use data.lstCountries for Countries List
        },
        error : function(error) {
            alert("Error: " + error.status);
        }
     });
于 2017-08-18T09:19:35.843 回答