我想在JSON文件中写入list的值。但是它给了一个例外。
JSONArray objJsonArray = null;
FileWriter objJsonFileWriter = null;
try {
objJsonArray = (JSONArray) JSONSerializer.toJSON(objList); //<- This line is giving net.sf.json.JSONException: There is a cycle in the hierarchy
objJsonFileWriter = new FileWriter("D:\\MyJson.json");
objJsonFileWriter.write(objJsonArray.toString());
objJsonFileWriter.flush();
objJsonFileWriter.close();
} catch (JSONException jse) {
jse.printStackTrace();
}
请让我知道如何摆脱这个例外。我正在使用核心Java
做这项工作答案 0 :(得分:2)
感谢@Kevin提出的宝贵建议。
JSONArray objJsonArray = null;
FileWriter objJsonFileWriter = null;
PlayerMasterJsonInfoBean objBean = null;
try {
objList.setFirst_name(getPlayerMaster.getFirst_name()); //<- Get the value from bean class then add to collection class
objList.setLast_name(getPlayerMaster.getLast_name()); //<- Get the value from bean class then add to collection class
objJsonArray = (JSONArray) JSONSerializer.toJSON(objList); //<- Then pass object here
objJsonFileWriter = new FileWriter("D:\\MyJson.json");
objJsonFileWriter.write(objJsonArray.toString());
objJsonFileWriter.flush();
objJsonFileWriter.close();
} catch (JSONException jse) {
jse.printStackTrace();
}