package com.matol.utils;import java.io.inputstream;import java.util.arraylist;import java.util.iterator;import java.util.linkedhashmap;import java.util.list;import java.util.map; import java.util.properties;import org.codehaus.jackson.map.objectmapper;/** * General-Purpose multilayer JSON recursive parsing. Mainly in the absence of object objects, or extremely complex multilevel nested JSON, the case can be in the way of a class, directly get the desired results. * supports data acquisition * usage of four returned objects in string, Map, ArrayList, Arraymap: Direct use based on JSON hierarchy: The base node. Child nodes. Sun Node * @author ww */public class jsonutil {private static String jsonStr = "{\" api\ ": \" 2.1\ ", \" message\ ": [\" product \ ", \" tokken\ "],\" request\ ": {\" ptype\ " : \ "Jk\", \ "tokken\": \ "a#dadsfkiwi239sdls#dsd\"},\ "response\": {\ "status\": {\ "statuscode\": \ "500\", \ " statusmessage\ ": [\" Product type error \ ", \" Tokken invalid \ "]},\" page\ ": {\" pagesize\ ": \" 100\ ", \" pageindex\ ": \" 1\ "},\" data\ ": {\" Ptitle\ ": \" All product lists\ ", \" sdate\ ": \" 2014-12-01\ ", \" edate\ ": \" 2016-12-01\ ", \" productlist\ ": [{\" pid\ ": \" ra001\ ", \" pname\ ": \" Product 1\ "},{\" pid\ ": \" ra002\ ", \" pname\ ": \" Product 2\ "}]}},\" Args\ ": [{\" tit\ ": \" ra001\ ", \" val\ ": \" Product 1\ "},{\" tit\ ": \" ra002\ ", \" val\ ": \" Product 2\ "}]}";p rivate static Objectmapper mapper = new objectmapper ();p Ublic static void main (String[] args) throws exception {//test obtains object objects through JSON//object obj = getobjectbyjson (Jsonstr, "Response.data.ptitle", typeenum.string); //hierarchy recursive String//system.out.println ("API:" +obj.tostring ()) ;//object obj = getobjectbyjson (Jsonstr, "Response.page", Typeenum.map); //hierarchy Recursive map// System.out.println ("API:" +obj.tostring () + ((Map) obj). Get ("pageSize"));//test Objectz turn json/*map mappars = new hashmap (); Mappars.put ("Agentcode", "SH0001"); Mappars.put ("Date", "2014-01-10"); Mappars.put ("url", "http://www.map.com/maps.jsp?tag=2"); Map mapargs = new hashmap (); Mapargs.put ("Query", &NBSp;mappars); Map maps = new hashmap (); Maps.put ("Request", mapargs); Maps.put ("Date", " 2014-10-10 "); System.out.println (Getobjectbyjson (MAPS)) *///test JSON-level recursive display effect Map maps = mapper.readvalue (jsonstr , map.class); Viewjsontree (maps);} /** complex nested map to Json */public static string getobjectbyjson (Object obj) {String str = ""; try {str = mapper.writevalueasstring (obj);} catch (exception e) {system.out.println ("###[error] getobjecttojson () " + E.getmessage ());} Return str;} /** complex nested JSON-level display */public static object viewjsontree (object m) {if (m = = null) { system.out.println ("over ..."); return false;} try {map mp = null; List ls = null;if (m instanceof map | | m instanceof linkedhashmap) {mp = (linkedhashmap) m;for (Iterator ite = MP.entryset (). iterator (); ite.hasnext ();) { map.entry e = (map.entry) Ite.next (); if (E.getvalue () instanceof string) {System.out.println ("[String]" +e.getkey () + " | " + e.getvalue ());} Else if (E.getvalue () instanceof linkedhashmap) {System.out.println ("{Map}" + e.getkey () + " | "+e.getvalue ()); Viewjsontree ((Linkedhashmap) e.getvalue ());} Else if (E.getvalue () instanceof arraylist) {System.out.println ("[Array]" + e.getkey () + " | "+e.getvalue ()); Viewjsontree ((ArrayList) e.getvalue ());}} }if (m instanceof list | | m instanceof arraylist) {ls = (ArrayList) m;for (Int i=0;i<ls.size (); i++) {if ( Ls.get (i) instanceof linkedhashmap) {Viewjsontree ((linkedhashmap) ls.get (i));} Else if (Ls.get (i) instanceof arraylist) {Viewjsontree ((ArrayList) ls.get (i));}}} System.out.println ();} catch (Exception e) {system.out.println ("###[error] viewjsontree () " +e.getmessage ());} Return null;} private int i = 0;/** complex nested JSON gets Object data */public Object Getobjectbyjson (String jsonstr,string argspath,typeenum argstype) {if (argsPath == null | | argspath.equals ("") | | argstype == null) {return null;} Object obj = null;try {map maps = mapper.readvalue (JsonStr, Map.class) ;//Multilayer Get if (Argspath.indexof (".") >= 0) {//Type Adaptive obj = getobject (Maps,argspath,argstype);} else{ //first layer gets if (argstype == typeenum.string) {obj = maps.get (Argspath). toString ();} Else if (ARGSTYPE&NBSP;==&NBSP;TYPEENUM.MAP) {obj = (map) maps.get (Argspath);} Else if (argstype == typeenum.arraylist) {obj = (List) maps.get (ArgsPath);} Else if (Argstype == typeenum.arraymap) {obj = (LIST< map>) Maps.get (Argspath);}} catch (exception e) {system.out.println ("###[error] getobjectbyjson () " + E.getmessage ());} I = 0;return obj;} Recursion gets Objectprivate object getobject (Object m,string key,typeenum type) {if (m == null) {return null;} object o = null; map mp = null; list ls = null;try {//Object-level recursive traversal parsing if (m instanceof map | | m instanceof linkedhashmap) {mp = (linkedhashmap) m;for (Iterator ite = Mp.entryset (). iterator (); ite.hasnext ();) { map.entry e = (map.entry) Ite.next (); if (i<key.split ("\ \"). Length && e.getkey (). Equals (Key.split ("\ \") [i])) {i++;if (E.getvalue () instanceof string) {if (i== key.split ("\ \."). Length) {o = e.getvalue (); return o;}} Else if (E.getvalue () instanceof linkedhashmap) {if (i== key.split ("\ \"). Length) {if (Type == typeenum.map) {o = (Linkedhashmap) e.getvalue (); return o;}} Else{o = getobject ((Linkedhashmap) E.getvalue (), key,type);} Return o;} Else if (E.getvalue () instanceof arraylist) {if (i== key.split ("\ \"). Length) {if (type == typeenum.arraylist) {o = (arrayList) e.getvalue (); Return o;} if (type == typeenum.arraymap) {o = (arraylist<map>) e.getvalue (); return o;}} Else{o = getobject ((ArrayList) E.getvalue (), key,type);} Return o;}}} }//array-level recursive traversal parsing if (m instanceof list | | m instanceof arraylist) {ls = (ArrayList) m;for (Int i=0;i<ls.size (); i++) {if ( Ls.get (i) instanceof linkedhashmap) {if (i== key.split ("\ \"). Length) {if (Type == typeenum.map) {o = (linkedhashmap) ls.get (i); return o;}} Else{o = getobject ((linkedhashmap) ls.get (i), key,type);} Return&nBsp;o;} Else if (Ls.get (i) instanceof arraylist) {if (i== key.split ("\ \"). Length) {if (type == typeenum.arraylist) {o = (arrayList) ls.get (i); return o;} if (type == typeenum.arraymap) {o = (arraylist<map>) ls.get (i); return o;}} Else{o = getobject ((ArrayList) ls.get (i), key,type);} Return o;}}}} catch (exception e) {system.out.println ("###[error] getobject () " +e.getmessage ( ));} Return o;} /* * json Data parsing return data type enumeration */public enum TypeEnum{/** simple key-value pair, get valus */by key string, /** Get to the Map object via key */ map, /** get to ArrayList array via key */ arraylist, /** via key for ARRaymap array object */ arraymap; }}
Generic multilayer JSON recursive parsing, used directly from the JSON hierarchy: the base node. Child node. Grandchild node.