Double-layer nested json string (json object embedded json array) resolved to Map, jsonmap
I wrote an article on the layer that introduced the conversion between json and map, but only involved a single json object or json array, the json string nested in the json array in the json object cannot be processed. This article mainly solves this problem.
The previous article address: http://blog.csdn.net/u012116457/article/details/24371877
First, import the json jar package in the project:
The following code uses both net. sf. json. JSONObject and org. json. JSONObject to process json objects.
First, create a pricejson.txt file under the e drive and write the following content:
{"Height": 1, "width": 1, "location": [{"TOP": "3" },{ "bottom": "1 "}, {"Left": "2" },{ "right": "1" },{ "floating": "4"}], "type ": [{"1": "1" },{ "2": "2" },{ "3": "4" },{ "4 ": "4"}]}
The following class reads the json string in the file through the read method and obtains the map through getMapByJson:
Package com. ngsh. common; import java. io. bufferedReader; import java. io. file; import java. io. fileInputStream; import java. io. fileNotFoundException; import java. io. IOException; import java. io. inputStreamReader; import java. io. unsupportedEncodingException; import java. util. hashMap; import java. util. list; import java. util. map; import net. sf. json. JSONObject; import org. json. JSONArray; public class FileIO {// read the file pub Lic String read (String path) {String data = ""; File file = new File (path); if (file. isFile () & file. exists () {try {InputStreamReader read = new InputStreamReader (new FileInputStream (file), "UTF-8"); // considering the encoding format BufferedReader bufferedReader = new BufferedReader (read ); string lineTxt = null; while (lineTxt = bufferedReader. readLine ())! = Null) {data + = lineTxt;} read. close ();} catch (UnsupportedEncodingException e) {e. printStackTrace ();} catch (FileNotFoundException e) {e. printStackTrace ();} catch (IOException e) {e. printStackTrace () ;}} return data ;}// convert json to map public Map <String, Object> getMapByJson (String json) {Map <String, object> map = new HashMap <String, Object> (); // The outermost layer parses JSONObject object = Object = JSONObject. fromObject (json); for (Object k: object. keySet () {Object v = object. get (k); map. put (k. toString (), v);} Map <String, Object> map2 = new HashMap <String, Object> (); // The second layer may or may not be for (Map. entry <String, Object> entry: map. entrySet () {try {JSONArray array = new JSONArray (entry. getValue (). toString (); // determines whether it is a json array. // It is a json array for (int I = 0; I <array. length (); I ++) {org. json. JSONObject object2 = array. getJSONObject (I); // json array object JSONObject object3 = JSONObject. fromObject (object2.toString (); // json Object for (Object k: object3.keySet () {Object v = object3.get (k); map2.put (k. toString (), v) ;}} catch (Exception e) {// not a json String Array map2.put (entry. getKey (), entry. getValue () ;}}/* for (Map. entry <String, Object> entry: map2.entrySet () {System. out. println (entry. getKey () + "-" + entry. getValue ();} */return map2;}/*** @ param args */public static void main (String [] args) {String path = "E: \ priceJson.txt "; FileIO fo = new FileIO (); Map map = fo. getMapByJson (fo. read (path); for (Map. entry <String, Object> entry: map. entrySet () {System. out. println ("key:" + entry. getKey () + "-value:" + entry. getValue ());}}}
The running result is as follows:
Key: 3-value: 4key: 2-value: 2key: 1-value: 1key: height-value: 1key: Left-value: 2key: 4-value: 4key: width-value: 1key: bottom-value: 1key: Floating-value: 4key: Right-value: 1key: top-value: 3
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.