-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9f19cd0
commit 115a0fb
Showing
2 changed files
with
65 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package com.learning.api.jsonnode; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
|
||
import com.fasterxml.jackson.databind.JsonNode; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
|
||
public class JsonNodeReader { | ||
|
||
public JsonNode readJsonFromString(String json) throws IOException { | ||
|
||
ObjectMapper objectMapper = new ObjectMapper(); | ||
return objectMapper.readTree(json); | ||
} | ||
|
||
public JsonNode readJsonFromFile(File file) throws IOException { | ||
|
||
ObjectMapper objectMapper = new ObjectMapper(); | ||
return objectMapper.readTree(file); | ||
} | ||
|
||
|
||
|
||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package com.learning.api.jsonnode; | ||
|
||
import java.util.Iterator; | ||
|
||
import com.fasterxml.jackson.databind.JsonNode; | ||
import com.fasterxml.jackson.databind.node.ArrayNode; | ||
|
||
public class ReadingFromJsonNode { | ||
|
||
String str; | ||
/* | ||
* It is very useful when you don't know the about Json what it looks like. | ||
* | ||
* Add operations as per your need. | ||
*/ | ||
public void traverseJson(JsonNode node) { | ||
|
||
if(node.isArray()) { | ||
ArrayNode arrayNode = (ArrayNode) node; | ||
for(int index=0;index<arrayNode.size();index++) { | ||
traverseJson(arrayNode.get(index)); | ||
} | ||
} | ||
else if(node.isObject()) { | ||
Iterator<String> fieldNames= node.fieldNames(); | ||
|
||
while(fieldNames.hasNext()) { | ||
str = fieldNames.next(); | ||
System.out.println(str); // -------Printing the Field --------- // | ||
traverseJson(node.get(str)); | ||
} | ||
} | ||
else { | ||
System.out.println(node.asText()); // ------Printing the field values--------// | ||
} | ||
|
||
} | ||
|
||
|
||
} |