Pallav Raj
Pallav Raj

Reputation: 1744

net.sf.json.JSONException: There is a cycle in the hierarchy

I want to write the value of list in JSON File. But it's giving an 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();
}

Please make me know how to get rid of this exception. I am doing this work using core Java

Upvotes: 3

Views: 5649

Answers (1)

Pallav Raj
Pallav Raj

Reputation: 1744

Thanks @Kevin for your valuable suggestion.

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

Upvotes: 2

Related Questions