fastjson removing an element according to a key

Data are as follows:

   [{ "ChapterId": 1, "sectionsName": "Introduction to Data structure", "id": 1, "contents": "ssssss"}, // this is a target sections
   { "chapterId":1,"sectionsName":"数据结构算法","id":2,"contents":"ssssss"},
   { "ChapterId": 1, "sectionsName": "Progressive Data Structure Analysis", "id": 3, "contents": "ssssss"}]

When taken in the database is a List <Sections> form removed, list put that Sections target, then converted to json array (this part of the Internet specifically noted) I am here is fastJson, before net.sf.json with the return at the front desk when the data transfer null pointer exception because there json null value, the null and fastJson to the deleted items.

List <Sections> sections = sectionsService.selectSectionsAll (chapter.getId ()); // get all the bar sections
JSONArray sectionsArray = (JSONArray) JSONArray.toJSON (sections); // array into json
for (int i = 0; i < sectionsArray.size(); i++) {
    JSONObject jsonObject = (JSONObject) sectionsArray.get (i); // get the object bar section array (JSON form)
    System.out.println("jsonObject: " + jsonObject);
    jsonObject.remove("contents");
    //jsonArray1.add(jsonObject);// will measure the object processed into the new json array, in fact, the original section of the array has been changed.          
   //System.out.println("jsonObject Hash: " + jsonObject.hashCode());
   //System.out.println("sections Hash: "+ sectionsArray.get (i) .hashCode ()); // hash address both is the same
}
JSONObject jsonObject2 = new JSONObject();
jsonObject2.put ( "first chapter", sectionsArray); // will be placed after the data processing in a new json, sectionsArray here already changed in the

In fact, here is the cock, the sectionsArray write up, and had thought it was wrong for the results. Later they found the two hash address is the same, do not know the other json tools are so?

Published 13 original articles · won praise 0 · Views 192

Guess you like

Origin blog.csdn.net/qq_24687915/article/details/104988044