프로젝트에 필요한 JAR 패키지는 온라인으로 찾기가 쉽지 않으므로 네트워크 디스크에 넣고 필요한 경우 다운로드합니다.
다운로드하려면 클릭하십시오
1. 간단한 구문 분석 JSON 줄
먼저 JSON 문자열을 JSON 객체로 변환 한 다음 JSON 객체를 구문 분석하십시오. 프로세스는 다음과 같습니다.
jsonobject jsonobject = jsonobject.fromobject (jsonst);
JSON의 키를 기반으로 값을 얻으십시오
문자열 이름 = jsonobject.getString ( "name"); int num = jsonobject.getint ( "num"); String sex = jsonobject.getstring ( "sex"); int age = jsonobject.getint ( "age");
2. JSON 문자열을 Java 객체로 변환합니다
또한 먼저 JSON 문자열을 JSON 객체로 변환 한 다음 아래와 같이 JSON 객체를 Java 객체로 변환하십시오.
jsonobject obj = new jsonobject (). fromObject (jsonst); // json 문자열 변환 JSON 개체
JSON 객체를 Java 객체로 변환하십시오
Person JB = (사람) JsonObject.Tobean (obj, person.class); // 빌드 된 JSON 개체를 사람 객체로 변환
3. Java 객체를 JSON 문자열로 변환하십시오
먼저 Java 객체를 JSON 객체로 변환 한 다음 JSON 객체를 JSON 문자열로 변환합니다.
jsonobject json = jsonobject.fromobject (obj); // java 객체를 json 객체 str = json.tostring ()로 변환
전체 코드는 다음과 같습니다.
패키지 baz.parse; java.util.arraylist 가져 오기; Java.util.list 가져 오기; import net.sf.json.json; import net.sf.json.jsonarray; import net.sf.json.jsonobject; import net.sf.json.jsonserializer; import baz.bean.person; 공개 클래스 Parsejson {private String jsonst; public parsejson () {} public parsejson (String str) {this.jsonst = str; } / ** * Parse JSON String * / public void parse () {jsonobject jsonobject = jsonobject.fromobject (jsonst); 문자열 이름 = jsonobject.getString ( "name"); int num = jsonobject.getint ( "num"); 문자열 sex = jsonobject.getString ( "섹스"); int age = jsonobject.getint ( "Age"); System.out.println (이름 + "" + num + "" + sex + "" + age); } // json 문자열을 Java 객체로 변환 공개 사람 json2object () {// receing {} 객체, 그리고 (jsonst.indexof ( "[")!)! = -1) {jsonstem = jsongs.replace ( "[", ",") 인 경우 배열 개체를 수신하는 데 대한 예외가 있습니다. } if (jsonst.indexof ( "]"! = -1) {jsonstem = jsonst.replace ( "]", ""); } JsonObject obj = new jsonObject (). fromObject (jsonst); // json 문자열 변환 json 객체 person jb = (person) jsonobject.tobean (obj, person.class); // 직함 객체를 사람으로 변환 jb; // 개인 개체}}}} 패키지 baz.bean; 공개 클래스 사람 {개인 문자열 이름; 개인 int Num; 개인 문자열 섹스; 사적인 int 연령; public person () {// todo 자동 생성 생성자 스텁} 공개 사람 (문자열 이름, int num, 문자열 섹스, int age) {super (); this.name = 이름; this.num = num; this.sex = 섹스; this.age = age; } public String getName () {return name; } public void setName (문자열 이름) {this.name = 이름; } public int getNum () {return num; } public void setnum (int num) {this.num = num; } public String getSex () {return sex; } public void setsex (String sex) {this.sex = sex; } public int getage () {반환 연령; } public void 설정 (int Age) {this.age = age; }} Java 객체를 JSON 문자열로 변환하십시오
패키지 baz.cons; import net.sf.json.jsonobject; /** * JSON String * @Author Administrator */public class consjson {public consjson () {// todo auto-auto-auto-auto-auto-auto-auto-auto-auto-auto-auto-auto-avj) {jsonobject json = jsonobject.fromobject (obj); json 객체 str = json str = json str = json str = json str = json java 객체 (jsonobject java 개체); 문자열 return str; }} 테스트 클래스 :
패키지 baz.test; Java.util.list 가져 오기; import baz.bean.person; import baz.cons.consjson; import baz.parse.parsejson; public class test {public static void main (String [] args) {// 문자열을 JSON 객체로 변환 한 다음 생성에 따라 해당 값을 얻습니다. Parsejson PJ = New Parsejson ( "{/"name/":/"gu/",/"num/": 123456,/"sex/":/"male/", 24}"); pj.parse (); // JSON 문자열을 Java 객체로 변환합니다 Person P = pj.json2object (); System.out.println ( "이름 :" + p.getName ()); System.out.println ( "num :" + p.getnum ()); System.out.println ( "섹스 :" + p.getSex ()); System.out.println ( "age :" + p.getage ()); // JSON 문자열로 JAVA 객체를 변환 P1 = New Person ( "GU1", 123, "Male", 23); consjson cj = new consjson (); 문자열 str1 = cj.object2json (p1); System.out.println (str1); }} 테스트 출력은 다음과 같습니다.
GU 123456 남성 24
이름 : GU
NUM : 123456
섹스 : 남성
나이 : 24
{ "age": 23, "name": "gu1", "num": 123, "sex": "male"}이것은 사용하는 가장 쉬운 방법이며 나중에 다른 용도를 업데이트 할 것입니다. 위는이 기사의 모든 내용입니다. 모든 사람의 학습에 도움이되기를 바랍니다. 모든 사람이 wulin.com을 더 지원하기를 바랍니다.