如何解决"无法编写内容:没有为类org.json.JSONObject找到序列化程序,也没有发现创建BeanSerializer的属性"?

时间:2017-09-20 06:31:43

标签: java arrays json logic

我正在创建应用程序,我需要返回从该数组创建的JSONArray或List。这是我的代码:

@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public List getProductsData(ProductsDataDto productsDataDto) {
    Connection conn = null;
    JSONObject jsonOutput = new JSONObject();
    Calendar calendar = Calendar.getInstance();
    org.json.JSONArray productList = new org.json.JSONArray();
    JSONArray array = new JSONArray();
    List productsList = new ArrayList<>();
    try {
    Gson gson = new GsonBuilder().create();
    String json = gson.toJson(productsDataDto);
    org.json.JSONObject jsonInput = new org.json.JSONObject(json);

    conn = createConnection(jsonInput);

    Statement sta = conn.createStatement();
    String nameFirstInsured = "";

    //////

    String nawQuery = "select * from naw where Naw_Id=" + productsDataDto.getNawId();

    ResultSet nawResultSet = sta.executeQuery(nawQuery);
    ResultSetMetaData metaDataNaw = nawResultSet.getMetaData();
    List personalDataList = new ArrayList();
    while (nawResultSet.next()) {
        nameFirstInsured = nawResultSet.getObject("Naw_Om_Naam").toString();
    }

    //////

    String Sql = "select * from Dekking where Adv_Id=" + productsDataDto.getAdvId() + "and Len_Id="
            + productsDataDto.getLenId();

    ResultSet m_ResultSet = sta.executeQuery(Sql);
    // List productList = new ArrayList();

    Map<String, Object> row = null;
    ResultSetMetaData metaData = m_ResultSet.getMetaData();
    Integer columnCount = metaData.getColumnCount();
    System.out.println("columnCount =============" + columnCount);
    while (m_ResultSet.next()) {
        row = new HashMap<String, Object>();
        String productCode = null;
        for (int i = 1; i <= columnCount; i++) {
            row.put("nameFirstInsured", nameFirstInsured);
            row.put("serviceProvider", "");
            prodcutClosedStatus(productsDataDto, row);
            String productname = m_ResultSet.getObject("Dek_Om_Produktnaam").toString();
            row.put("productCategory", productCode);
            row.put("productName", productname);
            row.put("productSubcategory", productname);
            row.put("productId", m_ResultSet.getObject("Dek_Id"));
            productCode = m_ResultSet.getObject("Dek_Om_Produktcode").toString();
            String date = m_ResultSet.getObject("Dek_Dt_Aanvang").toString();
            row.put("policyNumber", m_ResultSet.getObject("Dek_Om_Polisnummer"));
            row.put("startDate", date);
            if (productCode.equalsIgnoreCase("0/BEL")) {
                productCode = "Vermogen";
            } else if (productCode.equalsIgnoreCase("0/UIT")) {
                productCode = "Uitvaart";
            } else if (productCode.equalsIgnoreCase("0/LEV")) {
                productCode = "Kapitaalverz";
            } else if (productCode.equalsIgnoreCase("0/ORV") || productCode.equalsIgnoreCase("1/ORV")) {
                productCode = "ORV";
            } else if (productCode.equalsIgnoreCase("0/HLV")) {
                productCode = "AOV";
            } else if (productCode.equalsIgnoreCase("0/LYF")) {
                productCode = "Uit.lijfr";
            } else if (productCode.equalsIgnoreCase("0/SDV")) {
                productCode = "Schade";
            }
        }
        productList.put(row);
    }

    jsonOutput.put("productsData", productList);

    org.json.JSONArray productsArray = (org.json.JSONArray) jsonOutput.get("productsData");
    // productsArray.ge
    System.out.println(productsArray);

    for (int l = 0; l < productsArray.length(); l++) {
        System.out.println(productsArray.getJSONObject(l));
        org.json.JSONObject productsObject =  productsArray.getJSONObject(l);
        if (productsObject.get("productCategory").toString().equalsIgnoreCase("ORV")) {

            String sqlFirstInsurerName = "select v.dek_id, v.Anv_Id, a.Kln_Id,b.Naw_Id, n.Naw_Om_Naam from Verzekerde v inner join advies A on a.Adv_Id = v.Adv_Id inner join Aanvrager b on a.Kln_Id = b.Kln_Id and b.anv_id = v.Anv_Id inner join naw n on n.Naw_Id = b.Naw_Id where v.Adv_Id ="
                    + productsDataDto.getAdvId() + "and v.Len_Id =" + productsDataDto.getLenId()
                    + "and v.Dek_Id =" + productsObject.get("productId");

            ResultSet resultSetNames = sta.executeQuery(sqlFirstInsurerName);
            List nameList = new ArrayList();
            Map<String, Object> rowName = null;
            ResultSetMetaData metaDataName = resultSetNames.getMetaData();
            while (resultSetNames.next()) {
                String name = resultSetNames.getString("Naw_Om_Naam");
                Integer anvId = resultSetNames.getInt("Anv_Id");
                nameList.add(name);

                if (nameList.size() == 1 && anvId > 1) {
                    productsObject.put("nameFirstInsurer", name);
                }
                else {
                    productsObject.put("nameFirstInsurer", nameList.get(0));
                }
            }
        }
    }

    for (int i = 0; i < productList.length(); i++) {
      //  org.json.JSONObject jsonObj = (org.json.JSONObject) productList.get(i);
       // ObjectMapper mapper = new ObjectMapper();
        //User usr = mapper.readValue(jsonObj.toString(), User.class);   
       // Object obj = mapper.readValue(jsonObj.toString(), );
        productsList.add(productList.get(i));
    }

} catch (Exception e) {
    e.printStackTrace();
    String errorMessageAndClassWithMethod = getErrorContainingClassAndMethod();
    throw new SpringAppRuntimeException(errorMessageAndClassWithMethod + e.toString());
} finally {
    closeConnection(conn);
}
return productsList;

}

当我返回productList时,我收到错误:&#34; 无法写入内容:没有找到类org.json.JSONObject的序列化程序,也没有发现创建BeanSerializer的属性 &#34; 当我返回jsonOutput时,我得到的错误为&#34; 无法写入内容:没有为类org.json.JSONArray找到序列化程序,也没有发现要创建的属性BeanSerializer &#34;

我应该做些什么来成功返回jsonOutput或productList?

0 个答案:

没有答案