当前位置: 首页 > 知识库问答 >
问题:

无法编写JSON:找不到org.json.jsonObject类的序列化程序,也找不到创建BeanSerializer的属性

颛孙越
2023-03-14

无法编写JSON:找不到org.json.jsonObject类的序列化程序,也找不到创建BeanSerializer的属性(为了避免异常,禁用SerializationFeature.fail_on_empty_beans)

@RequestMapping(value = "/customerlist", method = RequestMethod.POST)
public ResponseGenerator getCustomerList() {
    ResponseGenerator responseGenerator = new ResponseGenerator();
    try {

        responseGenerator.setCode(StatusCode.SUCCESS.code);
        responseGenerator.setMessage(StatusCode.SUCCESS.message);
        responseGenerator.setStatus(ResponseStatus.SUCCESS);
        JSONObject  data =   userService.getUserList();
        responseGenerator.setJSONData(data);

        return responseGenerator; //error here

    } catch (Exception e) {

        logger.error("Error while getting Customer List : ", e);
        e.printStackTrace();
        responseGenerator.setCode(StatusCode.GENERAL_ERROR.code);
        responseGenerator.setMessage(StatusCode.GENERAL_ERROR.message);
        responseGenerator.setStatus(ResponseStatus.FAIL);

        return responseGenerator;  
    }
}

UserService.getUserList():

public JSONObject jsonResp;
public JSONObject getUserList() throws Exception{

    jsonResp =new JSONObject();
    //List<JSONObject> customers = new ArrayList<JSONObject>();
    JSONObject jsonResponse =   erpNextAPIClientService.getCustomerList();
    //ObjectMapper objectMapper = new ObjectMapper();
    //objectMapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);
    //JSONArray jsonArray = objectMapper.convertValue(jsonResponse.get("data"), JSONArray.class);
    JSONArray jsonArray = jsonResponse.getJSONArray("data");
    //JSONArray jsonArray =new Gson().fromJson(jsonResponse.get("data").toString(),JSONArray.class);
    for (int i = 0; i < jsonArray.length(); i++) {

        JSONObject cust =   erpNextAPIClientService.getUser(jsonArray.getJSONObject(i).get("name").toString());
        JSONObject custAddress =erpNextAPIClientService.getCustomerAddress(jsonArray.getJSONObject(i).get("name").toString());

        JSONObject custData = new JSONObject(cust.getString("data"));
        JSONObject custAddressData = new JSONObject(custAddress.getString("data"));

        custData.accumulate("bill_to_address_line_one",custAddressData.get("address_line1"));
        custData.accumulate("bill_to_address_line_two",custAddressData.get("address_line2"));
        custData.accumulate("bill_to_city",custAddressData.get("city"));
        custData.accumulate("bill_to_state",custAddressData.get("state"));
        custData.accumulate("bill_to_zip",custAddressData.get("pincode"));
        custData.accumulate("bill_to_country",custAddressData.get("country"));
        jsonResp.put("data",custData);
        System.out.println(custData.toString());    
        //customers.add(custData);
    }

    return jsonResp;

}

共有1个答案

钱修雅
2023-03-14

这将引发错误,因为JSONObject不公开默认的getter。尽管可以采取变通办法来避免这种情况。

您需要更改ResponseGenerator类以接受Map 而不是JSONObject。现在更改这一行:

responseGenerator.setJSONData(data);

对此:

 responseGenerator.setJSONData(data.toMap());
 类似资料: