Parse only one field in a large JSON string

前端 未结 2 1194
时光取名叫无心
时光取名叫无心 2021-01-27 04:41

I have a JSON string of the following format:

{

  \"foo\": \"small_vale\"  
  \"baz\": \"large_value\"
  \"bar\": \"another_large_value\"

}

H

2条回答
  •  悲哀的现实
    2021-01-27 05:09

    Your json file is not valid. Commas are missing. It should look like this:

    {
      "foo":"small_value",
      "baz":"large_value",
      "bar":"another_large_value"
    }
    

    This blog post says that Jackson or simple JSON are the fastest ways to parse big json data. Se the chapter "Big File Results" for reference.

    Example code for Jackson: Jackson JSON – Read Specific JSON Key

    It shows how to parse a json file ang get a value of a specific element.

    //read json file data to String
    byte[] jsonData = Files.readAllBytes(Paths.get("data.json"));
    
    //create ObjectMapper instance
    ObjectMapper objectMapper = new ObjectMapper();
    
    //read JSON like DOM Parser
    JsonNode rootNode = objectMapper.readTree(jsonData);
    JsonNode fooNode = rootNode.path("foo");
    System.out.println("foo value = "+fooNode.asText());
    

提交回复
热议问题