gpt4 book ai didi

java - 网络.sf.json.JSONException : There is a cycle in the hierarchy

转载 作者:行者123 更新时间:2023-11-30 09:12:09 25 4
gpt4 key购买 nike

我想在 JSON 文件中写入列表的值。但它给出了一个异常(exception)。

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();
}

关于java - 网络.sf.json.JSONException : There is a cycle in the hierarchy,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21746827/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com