无法编写JSON:找不到针对类org.json.JSONObject的序列化程序,也未找到创建BeanSerializer的属性 [英] Could not write JSON: No serializer found for class org.json.JSONObject and no properties discovered to create BeanSerializer

查看:138
本文介绍了无法编写JSON:找不到针对类org.json.JSONObject的序列化程序,也未找到创建BeanSerializer的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已将响应设置为JSON但得到了

I have set response as JSON but get this

无法编写JSON:找不到针对类org.json.JSONObject的序列化程序,也没有发现创建BeanSerializer的属性(为避免异常,请禁用SerializationFeature.FAIL_ON_EMPTY_BEANS)

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():

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. 尽管可以采取一种解决方法来避免这种情况.

This will throw an error, as JSONObject does not expose default getter. Although a workaround can be done to avoid this thing.

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

You need to change ResponseGenerator class to accept Map<String, Object> instead of JSONObject. Now change this line:

responseGenerator.setJSONData(data);

对此:

 responseGenerator.setJSONData(data.toMap());

我希望这应该起作用.

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

P.S.: My recommendation would be to remove JSONObject conversion and instead return an Object of actual class,as internally spring uses jackson, which is more powerful JSON framework then org.json

这篇关于无法编写JSON:找不到针对类org.json.JSONObject的序列化程序,也未找到创建BeanSerializer的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆