Previously, I wrote an article about JSON and map transformation, but only to a single JSON object or JSON array, JSON object nested inside this JSON array JSON string can not be processed, this article mainly solves this problem.
The previous article address: http://blog.csdn.net/u012116457/article/details/24371877
The first thing to do is to import the JSON jar package into your project:
In the following code, the JSON object is handled using both the Net.sf.json.JSONObject and the Org.json.JSONObject two packages.
First create a priceJson.txt on the E drive and write the content:
{ "Height":1, "width":1, " Location":[{" top ":"3" },{" bottom ":"1" },{ " left ":"2" },{" right ":"1" },{" suspended ":"4" }] , "type":[{"1": "1" },{"2":"2" },{" C7>3":" 4 " },{"4":" 4 " }]}
The following class reads the JSON string from the file through the Read method and gets to the map via Getmapbyjson:
PackageCom.ngsh.common;ImportJava.io.BufferedReader;ImportJava.io.File;ImportJava.io.FileInputStream;ImportJava.io.FileNotFoundException;ImportJava.io.IOException;ImportJava.io.InputStreamReader;ImportJava.io.UnsupportedEncodingException;ImportJava.util.HashMap;ImportJava.util.List;ImportJava.util.Map;ImportNet.sf.json.JSONObject;ImportOrg.json.JSONArray; Public class FileIO { //Read file PublicStringRead(String Path) {String data =""; File File =NewFile (path);if(File.isfile () &&file.exists ()) {Try{InputStreamReader Read =NewInputStreamReader (NewFileInputStream (file),"Utf-8");//Considering the encoding formatBufferedReader BufferedReader =NewBufferedReader (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 (); } }returnData }//Convert JSON to map PublicMap<string, object>Getmapbyjson(String JSON) {map<string, object> Map =NewHashmap<string, object> ();//Outermost parsingJsonobject Object = Object = Jsonobject.fromobject (JSON); for(Object K:object.keyset ()) {Object v = object.get (k); Map.put (K.tostring (), v); } map<string, object> map2 =NewHashmap<string, object> ();//Second layer resolution the second layer may or may not be for(Map.entry<string, Object> entry:map.entrySet ()) {Try{Jsonarray array =NewJsonarray (Entry.getvalue (). toString ());//Determine if it is a JSON array //is a JSON array for(inti =0; I < Array.Length (); i++) {Org.json.JSONObject object2 = Array.getjsonobject (i);//json Array ObjectsJsonobject 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 JSON string arrayMap2.put (Entry.getkey (), Entry.getvalue ()); } }/ * for (map.entry<string, object> entry:map2.entrySet ()) {System.out.println (Entry.getkey () + "-" +entry . GetValue ()); } */ returnMAP2; }/** * @param args * * Public Static void Main(string[] args) {String path="E:\\pricejson.txt"; FileIO fo =NewFileIO (); 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 results of the operation are 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: Suspension-value: 4Key: Right-value: 1Key: Top-value: 3
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Double-layered nested JSON strings (i.e. JSON arrays embedded in JSON objects) resolved to map