Convert complex JSON objects to map objects

Source: Internet
Author: User
Tags ticket

The recent project needs to connect with the customer a WebService interface, the customer that JSON string over, belong to the more complex JSON string, here to share with you in my project used in the Analytic method:

The method requires the following jar

 Packagecom.test;Importjava.util.ArrayList;ImportJava.util.HashMap;ImportJava.util.Iterator;Importjava.util.List;ImportJava.util.Map;ImportNet.sf.json.JSONArray;ImportNet.sf.json.JSONObject;/*** Description *@authorLittle boss * @created December 7, 2017 morning 9:08:03*/ Public classTestjson {/*** Description: Convert complex JSON object to map object *@authorThe Little boss * @created December 7, 2017 morning 9:15:12 *@paramJsonstr *@return     */@SuppressWarnings ("Unused")    Private StaticMap<string, object>Parsejson2map (String jsonstr) {Map<string, object> map =NewHashmap<string, object>(); //Outermost parsingJsonobject JSON =Jsonobject.fromobject (JSONSTR);  for(Object K:json.keyset ()) {Object V=Json.get (k); //if the inner layer is still an array, continue parsing            if(VinstanceofJsonarray) {List<map<string, object>> list =NewArraylist<map<string, object>>(); Iterator<JSONObject> it =( (Jsonarray) v). iterator ();  while(It.hasnext ()) {Jsonobject Json2=It.next ();                List.add (Parsejson2map (json2.tostring ()));            } map.put (K.tostring (), list); } Else{map.put (k.tostring (), v); }        }        returnmap; }          Public Static voidMain (string[] args) {String jsonstring= "{\" paycnt\ ": 3,\" payinfo\ ": [{\" payinfomain\ ": {\" ordnum\ ": \" 201206010000001\ ", \" transsite\ ": \" \ ", \" Transbankno \ ": \" tepb\ ", \" bankacctno\ ": \" 010\ ", \" totamt\ ": \" 30\ ", \" billchkcode\ ": \" 3316\ ", \" tolldeptno\ ": \" ndzsxh000\ ", \" Bustype\ ": \" Fjfs\ ", \" other1\ ": \" remark1\ ", \" other2\ ": \" remark2\ ", \" bankname\ ": \" analog bank 2\ ", \" transseq\ ": \" 20120601000000001\ ", \" trantype\ ": \" \ ", \" sitecode\ ": \" 200001\ ", \" ebillverno\ ": \" 000001\ ", \" tolldeptname\ ": \" Ningde Accounting Professional Technical examination Leading group \ ", \" itemcnt\ ": 1,\" tranuser\ ": \" \ "\" Payer\ ": \" Ningde \ ", \" ebillno\ ": \" 201206010000000136\ ", \" Transbankname\ ": \" analog bank 2\ ", \" transacctno\ ": \" 010\ ", \" sitename\ ": \" national judicial Examination Network \ ", \" orddate\ ": \" 20120601\ "},\" Ebillno \ ": \" 201206010000000136\ ", \" payinfoitem\ ": [{\" amt\ ": \" 30\ ", \" chrgstd\ ": \" 1\ ", \" cnt\ ": \" 1\ ", \" chrgname\ ": \" Junior Network engineer Registration fee \ ", \" msruint\ ": \" yuan/section \ ", \" chrgcode\ ": \" ka460001\ "}]},{\" payinfomain\ ": {\" ordnum\ ": \" 201206010000001\ " , \ "transsite\": \ "\", \ "transbankno\": \ "tepb\", \ "bankacctno\": \ "010\", \ "totamt\": \ "10\", \ "billchkcode\": \ "2258\" , \ "tolldeptno\": \ "777516727\",\ "bustype\": \ "Fjfs\", \ "other1\": \ "remark1\", \ "other2\": \ "remark2\", \ "bankname\": \ "analog bank 2\", \ "transseq\": \ " 20120601000000001\ ", \" trantype\ ": \" \ ", \" sitecode\ ": \" 200001\ ", \" ebillverno\ ": \" 000001\ ", \" tolldeptname\ ": \" Fujian provincial Capital Management office \ ", \" itemcnt\ ": 1,\" tranuser\ ": \" \ ", \" Payer\ ": \" contributor \ ", \" ebillno\ ": \" 201206010000000134\ ", \" Transbankname\ ": \" analog bank 2\ ", \" transacctno\ ": \" 010\ ", \" sitename\ ": \" national judicial Examination Network \ ", \" orddate\ ": \" 20120601\ "},\" Ebillno \ ": \" 201206010000000134\ ", \" payinfoitem\ ": [{\" amt\ ": \" 10\ ", \" chrgstd\ ": \" 1\ ", \" cnt\ ": \" 1\ ", \" chrgname\ ": \" Traffic confiscated \ ", \" msruint\ ": \" meta/section \ ", \" chrgcode\ ": \" 460\ "}]},{\" payinfomain\ ": {\" ordnum\ ": \" 201206010000001\ ", \" Transsite\ " : \ "\", \ "transbankno\": \ "tepb\", \ "bankacctno\": \ "010\", \ "totamt\": \ "20\", \ "billchkcode\": \ "0198\", \ "tolldeptno\ ": \" 003604520\ ", \" bustype\ ": \" Fjfs\ ", \" other1\ ": \" remark1\ ", \" other2\ ": \" remark2\ ", \" bankname\ ": \" analog bank 2\ ", \" Transseq\ ": \" 20120601000000001\ ", \" trantype\ ": \" \ ", \" sitecode\ ": \" 200001\ ", \" ebillverno\ ": \" 000001\ ", \" Tolldeptname\ ": \" Fuzhou Finance Bureau accounting management Department \ "ITEMCN"T\ ": 1,\" tranuser\ ": \" \ ", \" Payer\ ": \" Fuzhou \ ", \" ebillno\ ": \" 201206010000000135\ ", \" transbankname\ ": \" analog bank 2\ ", \" Transacctno\ ": \" 010\ ", \" sitename\ ": \" national judicial Examination Network \ ", \" orddate\ ": \" 20120601\ "},\" ebillno\ ": \" 201206010000000135\ ", \" Payinfoitem\ ": [{\" amt\ ": \" 20\ ", \" chrgstd\ ": \" 1\ ", \" cnt\ ": \" 1\ ", \" chrgname\ ": \" Accounting qualification certificate Examination fee \ ", \" msruint\ ": \" yuan/section \ " , \ "chrgcode\": \ "Ba460\"}]}]} "; Map<string, object> map=Parsejson2map (jsonstring); System.out.println (Map.get ("paycnt"));//number of electronic bills acquiredList List = (list) map.get ("Payinfo");//get e-ticket information (list type)Map T = (map) list.get (0);//get the first row of records, map typeMap Mainmap = (map) t.get ("Payinfomain");//get e-ticket key information, map typeString user = (string) mainmap.get ("payer");//Take out the contributor.System.out.println (user); }}

Tested Background Printing: 3 Ningde

Convert complex JSON objects to map objects

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.