Removing a json object from a json file in Java

后端 未结 4 1074
孤街浪徒
孤街浪徒 2021-01-22 13:25

I have this json file I downloaded online:

 {
\"price\": 1,
\"empty\": [
  0,
  0,
  0,
  0,
  0
],
\"lowValue\": 0,
\"highValue\": 0
},

and I

相关标签:
4条回答
  • 2021-01-22 13:55

    You can also parse your json by ignoring some fields. Look at this example:

    import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
    import com.fasterxml.jackson.core.JsonParseException;
    import com.fasterxml.jackson.databind.JsonMappingException;
    import com.fasterxml.jackson.databind.ObjectMapper;
    
    @JsonIgnoreProperties(value = { "empty" })
    public class Item {
    
        private long price;
        private long lowValue;
        private long highValue;
    
        public long getPrice() {
            return price;
        }
    
        public void setPrice(long price) {
            this.price = price;
        }
    
        public long getLowValue() {
            return lowValue;
        }
    
        public void setLowValue(long lowValue) {
            this.lowValue = lowValue;
        }
    
        public long getHighValue() {
            return highValue;
        }
    
        public void setHighValue(long highValue) {
            this.highValue = highValue;
        }
    
        @Override
        public String toString() {
            return "Item [price=" + price + ", lowValue=" + lowValue + ", highValue=" + highValue + "]";
        }
    
        public static void main(String[] args) throws JsonParseException, JsonMappingException, IOException {
    
            String file = "c:\\json";
    
            ObjectMapper mapper = new ObjectMapper();
    
            Item[] items = mapper.readValue(new File(file), Item[].class);
            for (Item item : items) {
                System.out.println(item);
            }
    
        }
    
    }
    

    c:\json contains:

        [
        {
            "price": 1,
            "empty": [
              0,
              0,
              0,
              0,
              0
            ],
            "lowValue": 0,
            "highValue": 0
        },
        {
            "price": 2,
            "empty": [
              0,
              0,
              0,
              0,
              0
            ],
            "lowValue": 3,
            "highValue": 4
        }
    ]
    

    Output is:

    Item [price=1, lowValue=0, highValue=0]
    Item [price=2, lowValue=3, highValue=4]
    
    0 讨论(0)
  • 2021-01-22 13:57

    Use the following code to remove element empty from json

    JSONObject jsonObject = (JSONObject) jsonParser.parse(new FileReader("File Path"));
    jsonObject .remove("empty");
    

    After removing empty element using jsonObject.toJSONString() to get target JSON, Now structure of JSON will be look like this

     {
    "price": 1,
    "lowValue": 0,
    "highValue": 0
    },
    
    0 讨论(0)
  • 2021-01-22 13:57

    Use a JSON library like Jackson:

    import com.fasterxml.jackson.databind.ObjectMapper;
    import com.fasterxml.jackson.databind.node.ObjectNode;
    
    import java.io.IOException;
    
    public class JsonDelete {
    
        public static void main(String[] args) throws IOException {
            ObjectMapper mapper = new ObjectMapper();
            String json = "{\"key\":\"value\",\"empty\":[]}";
    
            ObjectNode node = (ObjectNode) mapper.readTree(json);
            node.remove("empty");
    
            System.out.println(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(node));
        }
    
    }
    

    Outputs:

    {
      "key" : "value"
    }
    
    0 讨论(0)
  • 2021-01-22 14:07

    If you put a '[' at the beginning and a ']' at the end of json file, it becomes a valid json file. Like in your json file, It should be.

    [
        {
        "price": 1,
        "empty": [
          0,
          0,
          0,
          0,
          0
        ],
        "lowValue": 0,
        "highValue": 0
        },
        {
        "price": 500,
        "empty": [
          5,
          0,
          3,
          6,
          9
        ],
        "lowValue": 4,
        "highValue": 2
        }
    ]
    

    So the final program will be like:--

    public class ReadJSONFromFile {
        public static void main(String[] args) {
            JSONParser parser = new JSONParser();
            try {
                Object obj = parser.parse(new FileReader("locationOfFIle"));
                JSONArray array = (JSONArray) obj;
                FileWriter file = new FileWriter("locationOfFIle");
                for (int index = 0; index < array.size(); ++index) {
                    JSONObject jsonObject = (JSONObject) array.get(index);
                    jsonObject.remove("empty");
                    file.write(jsonObject.toJSONString());
                    file.flush();
                    if (index == array.size() - 1)
                        file.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题