Double-layer nested json string (json object embedded json array) resolved to Map

Source: Internet
Author: User

Double-layer nested json string (json object embedded json array) resolved to Map

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.
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}, {suspension: 4}], type: [{}, {}]}

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
  
   
GetMapByJson (String json) {Map
   
    
Map = new HashMap
    
     
(); // Parse the outermost layer JSONObject object = JSONObject. fromObject (json); for (Object k: object. keySet () {Object v = object. get (k); map. put (k. toString (), v);} Map
     
      
Map2 = new HashMap
      
        (); // The second layer of resolution may or may not be for (Map. Entry
       
         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
        
          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
         
           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

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.