Created
September 28, 2014 23:37
-
-
Save marcusdb/23f4d7bd394bd9a5cadd to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.javacodegeeks.java.core; | |
| import java.io.BufferedReader; | |
| import java.io.File; | |
| import java.io.FileReader; | |
| import java.io.IOException; | |
| import java.util.Iterator; | |
| import org.codehaus.jackson.JsonGenerationException; | |
| import org.codehaus.jackson.JsonNode; | |
| import org.codehaus.jackson.map.JsonMappingException; | |
| import org.codehaus.jackson.map.ObjectMapper; | |
| import org.codehaus.jackson.node.ObjectNode; | |
| public class JacksonTreeNodeTutorial { | |
| private static final String jsonFilePath = "C:\\Users\\nikos7\\Desktop\\filesForExamples\\mapExample.json"; | |
| public static void main(String[] args) { | |
| ObjectMapper mapper = new ObjectMapper(); | |
| try { | |
| FileReader fileReader = new FileReader(jsonFilePath); | |
| BufferedReader bufferedReader = new BufferedReader(fileReader); | |
| JsonNode rootNode = mapper.readTree(bufferedReader); | |
| JsonNode domainNode = rootNode.path("domain"); | |
| System.out.println("domain : "+domainNode.getTextValue()); | |
| JsonNode interestNode = rootNode.path("interest"); | |
| System.out.println("interest : " + interestNode.getTextValue()); | |
| JsonNode memebers = rootNode.path("members"); | |
| System.out.println("members : "+memebers.getIntValue()); | |
| JsonNode namesListNode = rootNode.path("names"); | |
| System.out.println("names : "); | |
| Iterator iterator = namesListNode.getElements(); | |
| while (iterator.hasNext()) { | |
| JsonNode temp = iterator.next(); | |
| System.out.println(" "+temp.getTextValue()); | |
| } | |
| ((ObjectNode)rootNode).put("domain", "wwww.javacodegeeks.com"); | |
| ((ObjectNode)rootNode).put("members", 600); | |
| ((ObjectNode)rootNode).remove("interest"); | |
| File file = new File(jsonFilePath); | |
| mapper.writeValue(file, rootNode); | |
| } catch (JsonGenerationException e) { | |
| e.printStackTrace(); | |
| } catch (JsonMappingException e) { | |
| e.printStackTrace(); | |
| } catch (IOException e) { | |
| e.printStackTrace(); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment