The first one we learned about the conversion between entities and JSON, but what we need is data you convert 18 times I can't get the data, so let's take a look at how to get a value from using Jackson from the JSON string. Nonsense not to say directly on the code (
Notice the method in the first article I moved to Jsonprocessutil. Easy to use)。
Remove the value of the specified node from the JSON string
Java code public static void getvalue (TESTVO VO) throws exception { // Preparation Incoming VO please refer to the entity in the first article. No more code here waste time ObjectMapper mapper = Jsonprocessutil.getmapperinstance (FALSE); string vojson = jsonprocessutil.tojson (VO); jsonnode node = mapper.readtree (Vojson);// Here the Jsonnode and the XML node are much like the system.out.println ("readvaluefromjson>>>" + node.get ("Voname"). ToString ();// get voname // Output:readvaluefromjson>>> a container just }
public static void GetValue (Testvo vo) throws Exception {
//preparatory work incoming VO refer to the entity in the first article. No more code here. Wasting Time
objectmapper mapper = Jsonprocessutil.getmapperinstance (false);
String Vojson = Jsonprocessutil.tojson (VO);
Jsonnode node = mapper.readtree (Vojson);//The node in this jsonnode and XML is very much like
System.out.println ("readvaluefromjson> >> "+ node.get (" Voname "). toString ();//Get voname
/output:readvaluefromjson>>> a container just
}
How simple, but this function is not very big ah. I want to get the value of the person's name under Testvo. Don't worry, let's go.
Java code public static void getvalue (TESTVO&NBSP;VO) throws exception { // preparation Incoming VO please refer to the entity in the first article. No more code here waste time ObjectMapper mapper = Jsonprocessutil.getmapperinstance (FALSE); string vojson = jsonprocessutil.tojson (VO); jsonnode node = mapper.readtree (Vojson);// Here the Jsonnode and the XML node are much like the node = node.get ("pers"); system.out.println ("node is not a collection:" + node.isarray ());// This way we'll use it later. Let's take a look at it first for (int i = 0; i < node.size (); i++) { JsonNode Childnode = node.get (i); &NBSP;&NBSP;&NBSp; system.out.println ("readvaluefromjson>>>" + childNode.get) ( "Name"). ToString ());// get name } /* * output * node is not a collection: True * readValueFromJson>>> John * readValueFromJson>>> Dick * Readvaluefromjson>>> Wang */ }
public static void GetValue (Testvo vo) throws Exception {
//preparatory work incoming VO refer to the entity in the first article. No more code here. Wasting Time
objectmapper mapper = Jsonprocessutil.getmapperinstance (false);
String Vojson = Jsonprocessutil.tojson (VO);
Jsonnode node = mapper.readtree (Vojson);//The node in Jsonnode and XML here is much like
node = node.get ("pers");
System.out.println ("node is not a set:" + Node.isarray ());//This method we will use later to let it give everyone a face for
(int i = 0; i < node.size (); i++) { C8/>jsonnode Childnode = Node.get (i);
System.out.println ("readvaluefromjson>>>" + childnode.get ("name"). toString ());//Get Name
}/
*
* Output Result
* node is not a collection: True
* readvaluefromjson>>> John
* readvaluefromjson>>> Dick
* readvaluefromjson>>> Wang-pock */
}
How to function is also OK. If so you are satisfied, then you are too easy to satisfy. Now I need to look at the value of a node in my project like XPath (don't know much about XPath. Don't worry, I will study with all of you later). /A/B/C/D this finds the value of a node. In order to increase the complexity, I added a list<person> childs to the person class; That means this guy's kids. So we have a lot of nodes to look down on ...
First, look at the data I've prepared.
Java code {"Voname": "A Container", "pers": [{"Name": "John", "Age": +, "childs": [{"Name": "Xiao Zhang 31", "Age": "Childs": null},{"name ":" Xiao Zhang 32 "," Age ":" Childs ": null}]},{" name ":" Dick "," Age ":", "childs": [{"Name": "Xiao Li 41", "Age": "Childs": null}]},{" Name ":" King II "," Age ":", "childs": null}]}
{"Voname": "A Container", "pers": [{"Name": "John", "Age": +, "childs": [{"Name": "Xiao Zhang 31", "Age": "Childs": null},{"name": " Xiao Zhang 32 "," Age ":" Childs ": null}]},{" name ":" Dick "," Age ":", "childs": [{"Name": "Xiao Li 41", "Age": "Childs": null}]},{" Name ":" King II "," Age ":", "childs": null}]}
John: There are two children dick: There is a king two-pock: no children
All I have to do now is to find out all the children.
No more nonsense, just look at the code.
Java code public static void main (String[] args) throws exception { // Preparation Data list<person> pers = new ArrayList<Person> (); list<person> childs = new arraylist<person > (); person p = new person ("John", 46); childs.add (New person ("Xiao Zhang 31", 20)); childs.add (New person ("Xiao Zhang 32", 17)); p.setchilds (Childs); pers.add (P); p = new person ("Dick", 29); childs = new ArrayList<Person> (); childs.add (New person ("Xiao Li 41", 20)); p.setchilds (Childs); pers.add (P); p = new person ("King of the two lepers", 23); pers.add (P); testvo vo = new testvo ("A container only", pers); // entity turn JSON string string json = beantojson (VO); object[] obj = readvaluefromjson (json, "Pers:childs: Name "). ToArray (); system.out.println (arrays.tostring (obj)); // output: [small John 1, small John 2, Xiao Li 41] }
public static void Main (string[] args) throws Exception {
//Prepare data
list<person> pers = new Arraylist<perso N> ();
List<person> childs = new arraylist<person> ();
Person p = new person ("John", +);
Childs.add (New Person ("Xiao Zhang 31");
Childs.add (New Person ("Xiao Zhang 32");
P.setchilds (childs);
Pers.add (p);
p = new Person ("Dick");
Childs = new arraylist<person> ();
Childs.add (New person ("Xiao Li 41");
P.setchilds (childs);
Pers.add (p);
p = new Person ("King of the two lepers");
Pers.add (p);
Testvo vo = new Testvo ("A container only", pers);
Entity spin JSON string string
json = Beantojson (VO);
object[] obj = Readvaluefromjson (JSON, "Pers:childs:name"). ToArray ();
System.out.println (arrays.tostring (obj));
Output result: [Xiao Zhang 31, Xiao Zhang 32, Xiao Li 41]
}
Let's have fun, and then we'll see how this readvalueformjson is achieved.
Java code/** * reading Tagpath values from JSON tagpath with : Separating * * @param json * @param tagpath * @return * @ Throws exception */ public static list<string> Readvaluefromjson (String json, string tagpath) throws exception { // return value list<string> value = new ArrayList<String> (); if (Commonutil.isempty (JSON) | | (Commonutil.isempty (Tagpath))) { return value; } ObjectMapper mapper = Commonutil.getmapperinstance (FALSE); string[] path =&Nbsp;tagpath.split (":"); jsonnode node = mapper.readtree (JSON); getjsonvalue (node, path, value, 1); return value; } Public static void getjsonvalue (jsonnode node, String[] path, list<string> values, int nextindex) { if (Commonutil.isempty (node)) { return;  } // is the end of the path directly value if (nextindex == path.length) { if (Node.isarray ()) { for (int i = 0; i < node.size (); i++) { jsonnode child = node.get (i). Get ( PATH[NEXTINDEX&NBSP;-&NBSP;1]); if (Commonutil.isempty (child)) { continue; } Values.add (Child.tostring ()); } } else { &Nbsp; jsonnode child = node.get (path[nextIndex - &NBSP;1]); if (! Commonutil.isempty (Child)) { values.add (child.tostring); } } return;  } // judgment is node is a collection or a node node = node.get (path[nextindex - 1]); if (Node.isarray ()) { for (Int i = 0; i < node.size () { i++) Getjsonvalue (Node.get (i), path, values, nextindex + 1); } } else { getjsonvalue (node, path, values, &NBSP;NEXTINDEX&NBSP;+&NBSP;1); } }
/** * Read Tagpath value from JSON tagpath: Separate * @param JSON * @param tagpath * @return * @throws Exception/P Ublic static list<string> Readvaluefromjson (string json, String Tagpath) throws Exception {//return value List<strin
g> value = new arraylist<string> (); if (Commonutil.isempty (JSON) | | (Commonutil.isempty (Tagpath))
{return value;
} objectmapper Mapper = Commonutil.getmapperinstance (false);
string[] Path = Tagpath.split (":");
Jsonnode node = mapper.readtree (JSON);
Getjsonvalue (node, path, value, 1);
return value; public static void Getjsonvalue (Jsonnode node, string[] path, list<string> values, int nextindex) {if (Common
Util.isempty (node)) {return; //Is the end of the path directly takes the value if (Nextindex = = path.length) {if (Node.isarray ()) {for (int i = 0; i < node.size (); i++)
{Jsonnode child = Node.get (i). Get (Path[nextindex-1]);
if (Commonutil.isempty (child)) {continue; } values.add (Child.tosTring ());
} else {Jsonnode child = Node.get (Path[nextindex-1]); if (!
Commonutil.isempty (Child)) {Values.add (child.tostring);
} return;
//The decision is whether node is a collection or nodes = Node.get (path[nextindex-1]); if (Node.isarray ()) {for (int i = 0; i < node.size (); i++) {Getjsonvalue (Node.get (i), path, values, Nextindex
+ 1);
} else {getjsonvalue (node, path, values, Nextindex + 1);
}
}
pretty good, although the function is realized. But children's shoes may have doubts about this, do you think he didn't take off his trousers and fart? First convert the bean to JSON and then take the value from the JSON. Inefficient, not to say, but also complex. I just go through the Get method much better ... If you think so, you are a thinker. But there is a situation (I encountered in the development) if the bean is very large different circumstances you need the field is not the same how do you do. To judge if else if else if? If there are 100 different situations. That would be exhausting. So in the database to configure different business conditions from different fields to take a good value, so that the use of this function. Additionally attached: Commonutil.isempty () method Java code/** * Determine if object is empty * * @param obj * @return */ public static boolean isempty (object obj) { boolean result = true; if (obj == null) { return true; &nbsP;&NBSP;&NBSP;&NBSP} if (obj instanceof string) { result = (obj.tostring (). Trim (). Length () == 0) | | obj.tostring (). Trim (). Equals ("null"); } else if (obj instanceof collection) { result = ((Collection) obj). Size () == 0; } else { result = ((obj == null) | | (obj.tostring (). Trim (). Length () < 1)) ? true : false; } return result; }
/**
* To determine if the object is empty
*
* @param obj
* @return
/Public
static Boolean IsEmpty (Object obj) {
Boolean result = true;
if (obj = = null) {return
true;
}
if (obj instanceof String) {result
= (obj.tostring (). Trim (). Length () = 0) | | obj.tostring (). Trim (). Equals ("null") ;
} else if (obj instanceof Collection) {result
= (Collection) obj). Size () = = 0;
} else {result
= (obj = n ull) | | (Obj.tostring (). Trim (). Length () < 1)) ? True:false;
}
return result;
}
Note: Jsonnode provides a number of ways to take values, but for general purpose I chose ToString () because of Gettextvalue (), Getintvalue () ... Some methods can only return values of a specified type, so that they do not achieve universal use. And there's a situation where I want the values of all the child nodes below the entire node. If you use the above method, you can only use ToString () when it returns the entire JSON string. Note: If the toString () method returns only the value of a property such as: name instead of an entire large node such as: Name,age,gender, then it adds double quotes to the returned string and takes care to remove the double quotes that begin at the end.
I'm going to learn about this today, and the next article will discuss with you why I create a Objectmapper object that's so complicated. Not directly new.