我想在JSON File中写入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进行这项工作
最佳答案
感谢@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();
}