도구 방법 :이 기사의 목적은 JSON 문자열을지도 키 값 쌍 스토리지로 변환하고 리프 노드의 데이터 만 저장하는 것입니다.
Maven은 Jar 패키지 버전을 참조합니다.
<pectionency> <groupid> org.json </groupid> <artifactid> json </artifactid> <bersion> 20090211 </version> </fectionency>
도구 :
package com.baofoo.admin.test; // com.alibaba.fastjson.jsonobject import; 가져 오기 lombok.extern.slf4j.slf4j; import org.junit.test; import 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 ( "호출 예외 : {}", e); e.printstacktrace (); } system.out.println ( "Come On !!!"); } @test public void test2 () 예외 {string str = "{/"result/":/"success/",/"message/":/"success! /"}"; 문자열 str2 = "{결과 : 성공, 메시지 : 성공}"; // jsonobject jsstr = jsonobject.parseobject (str); // system.out.println (jsstr); jsonobject obj = 새로운 jsonobject (str); 스택 <jsonobject> stobj = 새 스택 <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 : key value 쌍의 형태로 맵에 json 객체 데이터를 저장합니다. } jsonObject json = stobj.pop (); iterator it = json.keys (); while (it.hasnext ()) {String key = (string) it.next (); // 값의 값을 가져옵니다. 객체 값 = json.get (key); //system.out.println(value); if (jsonobject) {stobj.push ((jsonobject) value); // 재귀 적으로 jsontomap (stobj, resultmap); } else {resultMap.put (키, 값); }}} @test public void test3 ()는 예외를 겪고 {string jsonst = "{responseHeader : {status : 0, Qtime : 0}, SpellCheck : {China : {Numfound : 9, STARFOFFSET : 0, ENDOFFSET : 2," + "중국어, 중국 국제, 중국, 중국, 중국어, 중국어, 중국어, 중국어, 중국어, 중국어, 중국어, 중국어, 중국어, 중국어, 중국어, 중국어, 중국어, 중국어, 중국어, 중국어, 중국인 문화]}}, " +"Collations : {Collation : 중국의 산업 및 상업 은행}} "; jsonobject obj = new jsonobject (jsonst); 스택 <jsonobject> stobj = 새 스택 <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 웹 사이트를 지원해 주셔서 대단히 감사합니다!