Werkzeugmethode: Der Zweck dieses Artikels besteht darin, die JSON-Zeichenfolge in den Kartenspeicher zu konvertieren und nur die Daten des Blattknotens zu speichern
Maven verweist auf die JAR -Paketversion:
<Depopenty> <gruppe> org.json </Groupid> <artifactId> json </artifactId> <version> 20090211 </Version> </abhängig>
Werkzeuge:
Paket com.baofoo.admin.test; // importieren com.alibaba.fastjson.jsonObject; import lombok.extern.slf4j.slf4j; import org.junit.test; import org.json.*; import Java.util.*; /*** Erstellt von BF100 am 2018/4/12. */ @Slf4j public class testBycaoxNew {@test public void test1 () {try {int a = 1/0; } catch (Ausnahme e) {log.Error ("Ausnahme aufrufen: {}", e); E. printstacktrace (); } System.out.println ("Come on !!!"); } @Test public void test2 () löst Ausnahme aus {String str = "{/" result/":/" success/",/" message/":/" Erfolg! /"}"; String str2 = "{Ergebnis: Erfolg, Nachricht: Erfolg}"; // 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); Set <string> keys = resultMap.keyset (); für (String -Schlüssel: Schlüssel) {System.out.println (Schlüssel+":"+resultMap.get (Schlüssel)); }} / ** * @Author: SKS * @Description: Speichern Sie JSON-Objektdaten in der Karte in Form von Schlüsselwertpaaren, speichern Sie nur Blattknoten * @date: * / private statische void jsontomap (stack <jsonObject> stobj, map <String, Object> resultmap) througs {if (stoBj == {stoBj ==-stoBj ==-stoBj ==-stoBj ==-stoBj ==-stoBj ==-stoBj ==-stoBj ==-stoBj ==-stoBj ==-stobj. } JsonObject json = stobj.pop (); Iterator it = json.keys (); while (it.hasnext ()) {String key = (string) it.next (); // den Wert des Werts Objekt value = json.get (Schlüssel) abrufen; //System.out.println(Value); if (value Instance von jsonObject) {stobj.push ((jsonObject) value); // rekursiv JSontomap (stobj, resultMap) rekurse traverse; } else {resultMap.put (Schlüssel, Wert); } } } @Test public void test3() throws Exception{ String jsonStr ="{responseHeader:{status:0,QTime:0},spellcheck:{suggestions:{China:{numFound:9,startOffset:0,endOffset:2," + "suggestion:[Industrial and Commercial Bank of China, Chinese People, China International, China Agriculture, China Market, China Economy, Chinese People, China Broadcast, Chinesische Kultur]}}, " +" Kollationen: {Collation: Industrial and Commercial Bank of China}}} "; JsonObject obj = new JsonObject (JSONSON); Stack <JsonObject> stobj = new Stack <JsonObject> (); stobj.push (obj); MAP <string, object> resultmap = new HashMap <String, Object> (); Jsontomap (Stobj, resultMap); Set <string> keys = resultMap.keyset (); für (String -Schlüssel: Schlüssel) {System.out.println (Schlüssel+":"+resultMap.get (Schlüssel)); }}}Zusammenfassen
Das obige ist die Methode zum Konvertieren von JSON-Objekten in Kartenschlüsselwertepaare, die vom Editor eingeführt wurden. Ich hoffe, es wird für alle hilfreich sein. Wenn Sie Fragen haben, hinterlassen Sie mir bitte eine Nachricht und der Editor wird allen rechtzeitig antworten. Vielen Dank für Ihre Unterstützung auf der Wulin.com -Website!