加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – 无法编写JSON:没有为类org.json.JSONObject找到序列化

发布时间:2020-12-15 04:40:23 所属栏目:Java 来源:网络整理
导读:我已经将响应设置为 JSON但是得到了这个 Could not write JSON: No serializer found for class org.json.JSONObject and no properties discovered to create BeanSerializer (to avoid exception,disable SerializationFeature.FAIL_ON_EMPTY_BEANS) @Requ
我已经将响应设置为 JSON但是得到了这个

Could not write JSON: No serializer found for class org.json.JSONObject and no properties discovered to create BeanSerializer (to avoid exception,disable 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;

}

解决方法

这将抛出一个错误,因为JSONObject不公开默认的getter.
虽然可以采取一种解决方法来避免这种情况.

您需要更改ResponseGenerator类以接受Map< String,Object>而不是JSONObject.
现在改变这一行:

responseGenerator.setJSONData(data);

对此:

responseGenerator.setJSONData(data.toMap());

我希望这应该有效.

P.S.:我的建议是删除JSONObject转换,而是返回一个实际类的Object,因为内部spring使用jackson,这是更强大的JSON框架然后org.json

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读