递归查询复杂json (java)

使用net.sf.json的工具包来查询json其实已经很方便了,但在使用中遇到复杂多层的json,查找value时就感觉很麻烦,

所以,动手写了个递归查询的方法,二次封装net.sf.json的工具包,个人感觉挺好用,分享给大家,方法现在只支持简单的key和value的查询

project中要导入支持的jar


代码

方法主要是传了一个要jsonobject或jsonarray进去(用JSONObject.fromObject(....)或JSONArray.fromObject(...)得到的一个总的json对象),然后再传一个空的Map对象,Map的key放要查询的字段名称(在json里的key的名称),然后Map的所有value都放null,

程序自调用跑的时候会帮忙把查到的value放进去。然后就可以把value从Map里拿出来。简单吧,不需要管json是多少层的,是object还是array。

/**
     parameters: 
     * "Object o" is JSONObject or JSONArray that you converted
     *  from some javabean or string.....by net.sf.json 
     * 
     * "Map<String,Object> m" is a empty object, key is the column's 
     *  name that you want to find it out from json object, 
     *  value just set to null first, program will help to set 
     *  the value in.
     **/
    public void test2(Object o,Map<String,Object> m)
    {
        if(o.getClass().equals(JSONObject.class))
        {
           JSONObject temp= (JSONObject)o;
           for(String keyMap:m.keySet())
           {
               if(m.get(keyMap)==null)
               {
                    if(temp.containsKey(keyMap))
                    {
                        System.out.println("find out.....................");
                       // System.out.println(temp.get(keyCondition));
                        result=temp.get(keyMap);
                        m.put(keyMap, temp.get(keyMap));
                        //System.out.println(result);
                    }
                    else
                    {
                        Iterator it=temp.keySet().iterator();  
                         while(it.hasNext()){    
                               String key;    

                               key=it.next().toString();    
                               Object v=temp.get(key); 
                              if(v.getClass().equals(JSONObject.class)||v.getClass().equals(JSONArray.class))
                              {
                                  test2(v,m);
                              }
                          }   
                    }
               }
           }
          
        }
        else if(o.getClass().equals(JSONArray.class))
        {
            JSONArray tempArray=(JSONArray) o;
            for(Object ob:tempArray)
            {
                if(ob.getClass().equals(JSONObject.class))
                {
                    JSONObject tempJb=(JSONObject) ob;
                     for(String keyMap:m.keySet())
                     {
                        if(m.get(keyMap)==null)
                        {
                            if(tempJb.containsKey(keyMap))
                            {
                                System.out.println("find out....2222.................");
                                // System.out.println(tempJb.get(keyCondition));
                                 result=tempJb.get(keyMap);
                                  m.put(keyMap, tempJb.get(keyMap));
                            }
                            else
                            {
                                test2(tempJb,m);
                            }
                        }
                      
                     }
                 
                }
                else if(ob.getClass().equals(JSONArray.class))
                {
                    test2(ob,m);
                }
            }
        }
    }
测试代码

用arcgis map server给我们的json来测试吧,那个json复杂啊,它的特点就是够长够复杂。。。。干它!!


  public static void main(String[] args) {
        // TODO code application logic here
       try
       {
           /**
            
            **/
        String s="{\"displayFieldName\":\"STATE_NAME\",\"fieldAliases\":{\"STATE_NAME\":\"STATE_NAME\"},\"geometryType\":\"esriGeometryPolygon\",\"spatialReference\":{\"wkid\":4326},\"fields\":[{\"name\":\"STATE_NAME\",\"type\":\"esriFieldTypeString\",\"alias\":\"STATE_NAME\",\"length\":25}],\"features\":[{\"attributes\":{\"STATE_NAME\":\"Florida\"},\"geometry\":{\"rings\":[[[-80.785662408630856,28.785194039580265],[-80.762415224980032,28.736334643343969],[-80.832103938946204,28.786186115676191],[-80.85070497065243,28.785699864788143],[-80.747189861639356,28.398992369417602],[-80.492244587685263,27.870178580851277],[-80.509357946761497,27.825708709282427],[-80.481853213649629,27.845721638466305],[-80.401377098561738,27.703585541507724],[-80.357377881156765,27.555662240480434],[-80.239638200062245,27.264647630220782],[-80.190090801933366,27.185684549909325],[-80.223989537065222,27.215066035338086],[-80.289634846944978,27.241338648913811],[-80.326790894938824,27.24826202827202],[-80.290420512647074,27.212853387888305],[-80.221273303566818,27.202842282414828],[-80.147967174737403,27.109069331648314],[-80.090363201936498,26.974043675242179],[-80.114610116125846,26.973837534654717],[-80.050910778881473,26.797197740810109],[-80.084695637832738,26.326377572329367],[-80.127780858468213,25.977536432159162],[-80.193098833368254,25.760032379688685],[-80.30145585056647,25.613751741223354],[-80.329991218164537,25.490015115178799],[-80.30197564745032,25.401200762481373],[-80.416816713439175,25.249902782999207],[-80.42080711647526,25.19221944813567],[-80.5518761310218,25.212318665832029],[-80.693824908464364,25.152299021567298],[-80.856801149970238,25.185631039518061],[-80.975876981442184,25.130501358788273],[-81.119016169227962,25.134188786311885],[-81.14822996444542,25.164690234248202],[-81.183783875269853,25.268879590831339],[-81.140569224972523,25.320765038072373],[-81.011560664760054,25.214429702655902],[-80.951027159291243,25.202537535707211],[-80.915274610180916,25.246725346675451],[-80.97428163564183,25.322459998119864],[-81.143322806324591,25.396827343594026],[-81.258374802860772,25.681100053041117],[-81.19994716836419,25.71042229368604],[-81.256207149591887,25.803102821876006],[-81.53074216621178,25.91465834536325],[-81.718292774601366,25.923581578094961],[-81.705695643444756,26.000171970026226],[-81.794717116725508,26.111162414826207],[-81.813858580953649,26.284467110888269],[-81.849350956216881,26.332196959455541],[-81.864212321860805,26.439553979604824],[-81.942018766006754,26.467562567241927],[-81.968119231475669,26.517382553334663],[-81.928773233345112,26.534980840344719],[-81.884276745588068,26.642987423991752],[-81.773914390256891,26.710265827592966],[-81.897101967198466,26.663767159772284],[-81.944451966818633,26.550719665111597],[-82.021472922217754,26.524679910663213],[-82.083660091689808,26.715790679562001],[-82.068270884885692,26.766970327338015],[-82.051767526641356,26.866709082497998],[-82.09787264732438,26.921785524613256],[-81.991439987085471,26.96305657968616],[-81.979194174281417,27.031680175073948],[-81.992851089121871,27.03167641806025],[-82.012688241585295,26.976550608696375],[-82.153209776766346,26.937064867739352],[-82.257459681882096,27.004408952997828],[-82.282179322244303,27.024557385645096],[-82.2575818018978,26.997907070445255],[-82.17649535469991,26.913693275742091],[-82.153828705501326,26.790128826863281],[-82.289840607889403,26.849885895686697],[-82.355431307856861,26.948896048999568],[-82.398171771187151,26.998515605648766],[-82.368668611051476,26.947938605853324],[-82.343628689839704,26.903632729552907],[-82.380036668496757,26.947295861183949],[-82.506929959538638,27.236991542688322],[-82.531130783681363,27.260277268030126],[-82.514483358896996,27.210453693844727],[-82.569957499789325,27.274280185023123],[-82.532740280332575,27.331801874285329],[-82.565562302234184,27.386681465716205],[-82.685925355422683,27.4738446594111],[-82.638889133272869,27.503459256478163],[-82.665829042621681,27.49358698798121],[-82.681706096764287,27.524429344121483],[-82.48826701133811,27.478067046093631],[-82.514199704070961,27.511989629139201],[-82.427177049895704,27.52285923622717],[-82.57557165724603,27.512324786673798],[-82.6387670919583,27.536638132890754],[-82.569605340466126,27.552713122831719],[-82.55426231189989,27.582239625564636],[-82.626376738180909,27.555194057835596],[-82.54004081163157,27.608106055798345],[-82.552499829299336,27.644014080310207],[-82.404379562119288,27.791627868125669],[-82.398817725447884,27.90621939183762],[-82.460998446995802,27.940155229278432],[-82.483131719606945,27.821947511448592],[-82.510598111362697,27.831232039000341],[-82.538995363578692,27.935728089917479],[-82.645565243251383,28.028847073525753],[-82.650684672020191,28.007163247767554],[-82.698338999662212,28.04616892626856],[-82.672718878920705,28.010514310270029],[-82.700665998164027,27.975353558226029],[-82.644741232819342,27.966583945083826],[-82.725484997814078,27.94056278377364],[-82.56426451350427,27.878461936960598],[-82.623653104070854,27.848527047904312],[-82.644619137026098,27.715725079713994],[-82.678326539617217,27.705587817566389],[-82.793838572594552,27.829653943072511],[-82.728162886336634,27.717773220609747],[-82.741003344786918,27.685868276827076],[-82.84428558714086,27.850641323175239],[-82.779144086807094,28.173027433771878],[-82.673475942404252,28.428510627506814],[-82.636075773228185,28.692748912872464],[-82.68138067327402,28.808382207542902],[-82.636519015003557,28.814277962730309],[-82.636619176925819,28.884707940488958],[-82.755322238603696,29.00866034179726],[-82.802156948696165,29.155132383040439],[-83.03674776741741,29.179387751877407],[-83.073928722800304,29.265652288465311],[-83.142206512197561,29.299651430965703],[-83.108995073529059,29.32818284309888],[-83.174632813916048,29.343679210503357],[-83.234327785147798,29.433937504499731],[-83.38003129986248,29.519874768983257],[-83.404973861273334,29.669602600216987],[-83.550448241669756,29.737323154463869],[-83.654182722500764,29.91096182669579],[-83.971738329597258,30.077483109510425],[-84.074625059068296,30.099948213350547],[-84.147854801380561,30.08172495898253],[-84.23307130990554,30.108111696682343],[-84.353924908202998,30.069624418832433],[-84.360416651392597,29.977390732119272],[-84.437690091488491,29.991791514322529],[-84.433104147467418,29.959545228465355],[-84.338938702170708,29.947141513065375],[-84.346911138444057,29.910168726673337],[-84.464537855155299,29.929627272364705],[-84.858030735784041,29.746860643189802],[-84.928513316535202,29.777971894822326],[-84.988283115342483,29.719921500132283],[-85.214759973181174,29.701557659090987],[-85.364879069737356,29.683019068575106],[-85.409603918748417,29.77676635866468],[-85.414051994296756,29.863067625344303],[-85.392994381602307,29.875413458867609],[-85.402546587284576,29.79436493721246],[-85.358043047591025,29.691254723017074],[-85.306589442077609,29.701710331650911],[-85.300524131480302,29.809796481829597],[-85.384792579465568,29.923801476891047],[-85.628497480128431,30.092591246777985],[-85.568528653325018,30.098293676516388],[-85.471357955651285,30.021863582634193],[-85.41501336574612,30.031552423879781],[-85.432385945142528,30.045754283267254],[-85.38269439688429,30.024270193612924],[-85.382656236793707,30.036286198139415],[-85.395672307944949,30.058567367183379],[-85.464315805839021,30.051163547940337],[-85.529716744458597,30.131686494748219],[-85.709829467923157,30.178773444062195],[-85.66605839996231,30.251689426742491],[-85.600176436683512,30.251151056224575],[-85.569475508371397,30.311004067351035],[-85.717131267403659,30.265061931219037],[-85.752708285064955,30.296929416122762],[-85.850420534832097,30.280359378408775],[-85.829942633171868,30.232813997853199],[-85.757919175717689,30.228945498235746],[-85.726393260157124,30.128842187452413],[-85.987013256278601,30.274430446602068],[-86.387459591957793,30.387658864096167],[-86.505313817348167,30.409972908765248],[-86.38810807865292,30.406012830646514],[-86.240033039601883,30.399887786275389],[-86.241566478263337,30.428527790749072],[-86.114640755007997,30.385801871067851],[-86.122933984284202,30.426461679026133],[-86.219608295894034,30.487854218791004],[-86.387879104635545,30.462167603881639],[-86.452639102654189,30.501237134862315],[-86.610235816371926,30.423651605797691],[-86.790347611484833,30.417963339703718],[-87.193382081550681,30.355221808002963],[-86.93251078493779,30.463564937878601],[-87.014399895011366,30.51443454398424],[-86.986193153267834,30.590430608223578],[-87.019580452024343,30.587466663968581],[-87.069271843349782,30.450564545647641],[-87.124533654029392,30.564666780390503],[-87.171555058559989,30.557735613068072],[-87.160102583226944,30.465033184653745],[-87.273897602679938,30.357384619533121],[-87.424080118729933,30.323671494544374],[-87.409256149896066,30.402238860720878],[-87.346807442263355,30.431498736968219],[-87.347395122613534,30.457169125435151],[-87.418816650734016,30.48170078231518],[-87.445352964424615,30.531389702818981],[-87.422067363882547,30.556493607802125],[-87.393280967014562,30.620098263190116],[-87.398644917452145,30.66801533057118],[-87.418894271998624,30.692810185026094],[-87.460140489783612,30.705802636039991],[-87.52660286942853,30.748491695828086],[-87.541900137063919,30.785695340645518],[-87.615915154575987,30.848296422522459],[-87.625711591394008,30.876901994344784],[-87.589867228991693,30.954361029749276],[-87.598580256289949,31.002630772766423],[-87.163117593206721,31.003157230384403],[-86.779361705206497,30.99819181074206],[-86.701852488684722,30.998092409081988],[-86.383919743637136,30.991538305687893],[-86.181476168036284,30.995225124787424],[-86.031821792675544,30.993327600280683],[-85.486597125301557,31.000998006462449],[-85.485010204985088,31.001001839164424],[-85.001607197633291,31.001253399467323],[-85.000141999727532,30.979319451750527],[-84.97356770002358,30.963664378591897],[-84.969012270571852,30.927093775105625],[-84.93777614957321,30.894957638456614],[-84.926926238719204,30.846922175553285],[-84.93256395069335,30.802625045154457],[-84.926894843192827,30.776088703458267],[-84.91534341344493,30.75408980757922],[-84.888288789745275,30.743889381659486],[-84.864583069596165,30.714503202203023],[-84.863003711177612,30.712664538143475],[-84.374448160922327,30.694091875139975],[-84.281663308192947,30.690419134502193],[-84.075562217076936,30.678577599233932],[-84.000730151896107,30.675537659869761],[-83.737843939848801,30.66049119015943],[-83.609056056922213,30.650784591510497],[-83.310600738669791,30.634303236699157],[-83.301315482327936,30.633284937123502],[-83.132440380338892,30.621341397914826],[-82.696355959895072,30.595720098417438],[-82.582957578270168,30.589053790731402],[-82.463134883250291,30.58268983690164],[-82.412092670919776,30.577307520006258],[-82.221039570309642,30.567076028208106],[-82.23839614808611,30.531444742235028],[-82.219367483588314,30.502970537265941],[-82.19918717195111,30.490014500624728],[-82.205564825039403,30.423853154425931],[-82.180234107813476,30.368630919532283],[-82.164639187551927,30.361291833302644],[-82.052766813556445,30.36379445400636],[-82.038354755363798,30.378904577731657],[-82.046099494573923,30.434068156845722],[-82.035120676036343,30.442878324408372],[-82.022509492199561,30.477717837000874],[-82.005801628153336,30.570990509623147],[-82.013652686873002,30.598690537335507],[-82.044911618846982,30.650910763440262],[-82.031881196090183,30.757532764430366],[-82.01296733599014,30.764256413605825],[-82.015538717102004,30.791319247127451],[-81.981479881952112,30.778351732009227],[-81.961551372170163,30.796007770380584],[-81.953029209458464,30.820430960744165],[-81.943804995059054,30.824249426368524],[-81.90751835875443,30.81347506462528],[-81.899141093328012,30.828308293955907],[-81.873001787647695,30.799084403628235],[-81.803198160572038,30.788084979368318],[-81.776707939598793,30.761584654923659],[-81.757710175420186,30.769631669390911],[-81.736675023597854,30.763888840479062],[-81.716768818345599,30.745368895720215],[-81.642477517444092,30.729080986269967],[-81.627980997644954,30.731774261411697],[-81.604771015642427,30.71632128022906],[-81.601169890818369,30.724887129247662],[-81.536591271136615,30.706577298330203],[-81.52859548779162,30.721452651940378],[-81.498784259367937,30.598605356040569],[-81.518117459779148,30.556212851284574],[-81.457528746207458,30.454764254446776],[-81.480798304068401,30.380540412334522],[-81.443259987959095,30.357187370868434],[-81.432767663500499,30.246781758262088],[-81.302757874470046,29.91305201484327],[-81.316489883748716,29.829240287381428],[-81.243784473163544,29.73794312590854],[-81.233765833276152,29.669023892412447],[-81.10988029543546,29.430239259547541],[-81.097054570676846,29.351799316636324],[-80.785662408630856,28.785194039580265]],[[-86.833882266890896,30.399735193816884],[-86.791553072342651,30.403969463970334],[-86.620825669382498,30.414551816751619],[-86.534756367412797,30.40679139269642],[-86.522770294313943,30.401147735536536],[-86.532642968319578,30.393388780539802],[-86.738641643951951,30.402558168746598],[-86.79296453355721,30.390564943790707],[-86.834584163827287,30.387038276729641],[-87.2423486031621,30.321431846955445],[-87.267045824424599,30.321431878929364],[-87.290324105063576,30.332719378491404],[-87.286089688590209,30.339772583724667],[-87.202140700129291,30.334834582138271],[-87.139349167319779,30.35176588410803],[-87.047641382361576,30.369401044061753],[-86.92912289596768,30.381394410941279],[-86.833882266890896,30.399735193816884]],[[-80.735833421380335,28.788844372027395],[-80.763330341641449,28.82206782455382],[-80.817179810121729,28.895390594509166],[-80.891079384762975,29.01339383810593],[-80.916287225971416,29.07182226897336],[-80.899097830760581,29.061511337282724],[-80.862438029722512,28.991625673169203],[-80.735833421380335,28.788844372027395]],[[-80.725899748375923,28.784365995696859],[-80.579689196540926,28.590154059379781],[-80.52594806493363,28.46346262811689],[-80.587655856129345,28.409260190852102],[-80.62163023653244,28.412659142910538],[-80.577446689976512,28.548653436918784],[-80.599129071182347,28.603922825531473],[-80.660043130106601,28.618651108231493],[-80.691881559555725,28.588350223976477],[-80.780445268525511,28.618960611503578],[-80.785930256531401,28.687716260901141],[-80.734994790998144,28.706617157996956],[-80.754068401720701,28.736883887839763],[-80.641197673111833,28.657346011842286],[-80.725899748375923,28.784365995696859]],[[-80.688623824655849,28.581764362332652],[-80.653794756347224,28.600890459148104],[-80.60919283666982,28.573566526298102],[-80.662730548928209,28.427410206958356],[-80.667110569587351,28.301841270657661],[-80.605571665409002,28.144944616554753],[-80.721043479320485,28.385401015233704],[-80.688623824655849,28.581764362332652]],[[-82.101941548046,26.586077178255238],[-82.066953017953949,26.497564370630322],[-82.097456086124112,26.493942546948379],[-82.13570958488998,26.642629854817127],[-82.183310145212772,26.683432364070477],[-82.128651830794411,26.693691330964914],[-82.101941548046,26.586077178255238]],[[-80.249453677873134,25.354937642313288],[-80.355665273094928,25.158233598866115],[-80.5878197219821,24.956376399079556],[-80.353063681558581,25.211532159148277],[-80.330296991451576,25.267981534347417],[-80.361601138010144,25.296499251188337],[-80.249453677873134,25.354937642313288]]]}}]}";
    
        TestJson tj=new TestJson();
         JSONObject job=JSONObject.fromObject(s);
         Map<String,Object> m=new HashMap<String,Object>();
         m.put("rings",null);
          m.put("STATE_NAME",null);
         m.put("fieldAliases",null);
        tj.test2(job,m);
        System.out.println(m.get("rings"));
        System.out.println(m.get("STATE_NAME"));
        System.out.println(m.get("fieldAliases"));.......
好了吧,挺爽~~~~


展开阅读全文

没有更多推荐了,返回首页