How to parse JSON file?

后端 未结 3 886
礼貌的吻别
礼貌的吻别 2020-12-09 05:49

Simple situation -

  1. read a json file
  2. discover all key-value pairs
  3. compare key-value pairs

I tried gson, package from json.org,

3条回答
  •  孤城傲影
    2020-12-09 06:36

    We use Jaskson parser, here are the sample code:

    protected T getJsonObject(InputStream inputStream, Class className) throws JsonParseException,
          JsonMappingException, IOException {
        // Deserialize input to Json object
        ObjectMapper mapper = new ObjectMapper();
    
        T jsonSource = mapper.readValue(inputStream, className);
        return jsonSource;
    }
    

    Here is the code how to invoke it:

    JsonEmployee jsonEmployee = getJsonObject(inputStream, JsonEmployee.class);
    

    JsonEmployee.java is just POJO

提交回复
热议问题