Java – unable to write JSON: not for class org json. Jsonobject found the serializer and did not find the property to create beanserializer
•
Java
I've set the response to JSON, but I get this
@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; }
Solution
This will throw an error because jsonobject does not expose the default getter
You need to change the responsegenerator class to accept map < string, Object > instead of jsonobject Now change this line:
responseGenerator.setJSONData(data);
In this regard:
responseGenerator.setJSONData(data.toMap());
I hope this should work
P. S.: my suggestion is to delete the JSON object transformation and return an object of the actual class, because the internal spring uses Jackson, which is a more powerful JSON framework, and then org json
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码