-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReadingFromJsonNode.java
74 lines (55 loc) · 1.69 KB
/
ReadingFromJsonNode.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package com.learning.api.jsonnode;
import java.util.Iterator;
import java.util.Map.Entry;
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 {
if(node != null && (!node.isNull())) // Null check. If node has null values it will not print.
System.out.println(node.asText()); // ------Printing the field values--------//
}
}
public void readingFieldNameAndValuesFromJsonNode(JsonNode node ) {
Iterator<Entry<String,JsonNode>> it = node.fields();
while(it.hasNext()) {
Entry<String,JsonNode> entry = it.next();
System.out.println(entry.getKey()+ " : " + entry.getValue());
}
}
public void readingFieldValueFromJsonNode(JsonNode node ) {
Iterator<String> it = node.fieldNames();
while(it.hasNext()) {
String field= it.next();
System.out.println(node.get(field));
}
}
public String getJsonNodeFieldAtPath(JsonNode node, String path) {
String str;
str = node.at(path).asText();
if(str.isEmpty() || str==null)
return "";
else
return str;
}
}