Java 无法编写 JSON:未找到类 org.json.JSONObject 的序列化程序,也未发现用于创建 BeanSerializer 的属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45915851/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 02:14:16  来源:igfitidea点击:

Could not write JSON: No serializer found for class org.json.JSONObject and no properties discovered to create BeanSerializer

javajsonspring

提问by Sandip Solanki

I have set response as JSON but get this

我已将响应设置为 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)

无法写入 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():

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;

}

采纳答案by Sach141

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

这将引发错误,因为JSONObject不会公开 default getter。尽管可以采取一种解决方法来避免这种情况。

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

您需要将ResponseGeneratorclass更改为 acceptMap<String, Object>而不是JSONObject. 现在改变这一行:

responseGenerator.setJSONData(data);

to this:

对此:

 responseGenerator.setJSONData(data.toMap());

I hope this should work.

我希望这应该有效。

P.S.: My recommendation would be to remove JSONObjectconversion and instead return an Object of actual class,as internally spring uses Hymanson, which is more powerful JSONframework then org.json

PS:我的建议是删除JSONObject转换,而是返回实际类的对象,正如内部 spring 使用的那样Hymanson,这是一个更强大的JSON框架org.json

回答by Pramuditha

Try with this in the entity class. It solved that issue.

在实体类中试试这个。它解决了这个问题。

@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})