ツール方法:この記事の目的は、JSON文字列をマップキー値ペアストレージに変換し、リーフノードのデータのみを保存することです。
MavenはJARパッケージバージョンを参照してください:
<Dependency> groupId> org.json </groupId> <Artifactid> json </artifactid> <version> 20090211 </version> </dependency>
ツール:
パッケージcom.baofoo.admin.test; // com.alibaba.fastjson.jsonobjectをインポートします。 Lombok.extern.slf4j.slf4jをインポートします。 Import org.junit.test; org.json。*; Java.util。*をインポートします。 /*** 2018/4/12にBF100によって作成されました。 */ @slf4j public class testbycaoxnew {@test public void test1(){try {int a = 1/0; } catch(例外e){log.error( "call exception:{}"、e); e.printstacktrace(); } system.out.println( "come on !!!"); } @test public void test2()スロー例外{string str = "{/" result/":/" success/"、/" message/":/" success! /"}"; string str2 = "{result:success、message:success}"; // jsonobject jsstr = jsonobject.parseobject(str); // System.out.println(JSSTR); jsonObject obj = new JSonObject(str); stack <jsonobject> stobj = new stack <jsonobject>(); stobj.push(obj); map <string、object> resultmap = new Hashmap <string、object>(); jsontomap(stobj、resultmap); <string> keys = resultmap.keyset()を設定します。 for(string key:keys){system.out.println(key+":"+resultmap.get(key)); }} / ** * @author:sks * @description:jsonオブジェクトデータをキー価値のペアの形でマップに保存し、葉のノードのみを保存します * @date: * / private static void jsontomap(stack <jsonobjed> stobj、map <<string> cultmap)throws {stobj = = null&&lull ; } jsonobject json = stobj.pop(); iterator it = json.keys(); while(it.hasnext()){string key =(string)it.next(); //値オブジェクト値の値を取得= json.get(key); //system.out.println(value); if(value instanceof jsonobject){stobj.push((jsonobject)value); // jsontomap(stobj、resultmap)を再帰的にトラバースします。 } else {resultmap.put(key、value); }}} @test public void test3()throws exception {string jsonstr = "{responseheder:{status:0、qtime:0}、spelcheck:{spossions:{numfound:9、startoffset:0、endoffset:2、" + " +" + " +中国経済、中国国民、中国農業、中国農業、中国農業、文化]}}、 " +" collations:{collation:産業および商業銀行の中国銀行}}} "; jsonobject obj = new JSonObject(JSonstr); stack <jsonobject> stobj = new stack <jsonobject>(); stobj.push(obj); map <string、object> resultmap = new Hashmap <string、object>(); jsontomap(stobj、resultmap); <string> keys = resultmap.keyset()を設定します。 for(string key:keys){system.out.println(key+":"+resultmap.get(key)); }}}要約します
上記は、JSONオブジェクトをエディターが導入したマップキー値ペアに変換する方法です。私はそれが誰にでも役立つことを願っています。ご質問がある場合は、メッセージを残してください。編集者は、すべての人に時間内に返信します。 wulin.comのウェブサイトへのご支援ありがとうございます!