工程中所需的jar包,由於在網上不太好找,因此我將它放到個人網盤裏了,若有須要隨便下載。java
點擊下載json
首先將json字符串轉換爲json對象,而後再解析json對象,過程以下。數組
JSONObject jsonObject = JSONObject.fromObject(jsonStr);測試
根據json中的鍵獲得它的值this
String name = jsonObject.getString("name"); int num = jsonObject.getInt("num"); String sex = jsonObject.getString("sex"); int age = jsonObject.getInt("age");
一樣先將json字符串轉換爲json對象,再將json對象轉換爲java對象,以下所示。spa
JSONObject obj = new JSONObject().fromObject(jsonStr);//將json字符串轉換爲json對象code
將json對象轉換爲java對象對象
Person jb = (Person)JSONObject.toBean(obj,Person.class);//將建json對象轉換爲Person對象字符串
先將java對象轉換爲json對象,在將json對象轉換爲json字符串get
JSONObject json = JSONObject.fromObject(obj);//將java對象轉換爲json對象
String str = json.toString();//將json對象轉換爲字符串
完整代碼以下:
package baz.parse; import java.util.ArrayList; import 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; public class ParseJson { private String jsonStr; public ParseJson() { } public ParseJson(String str){ this.jsonStr = str; } /** * 解析json字符串 */ public void parse(){ JSONObject jsonObject = JSONObject.fromObject(jsonStr); String name = jsonObject.getString("name"); int num = jsonObject.getInt("num"); String sex = jsonObject.getString("sex"); int age = jsonObject.getInt("age"); System.out.println(name + " " + num + " " + sex + " " + age); } //將json字符串轉換爲java對象 public Person JSON2Object(){ //接收{}對象,此處接收數組對象會有異常 if(jsonStr.indexOf("[") != -1){ jsonStr = jsonStr.replace("[", ""); } if(jsonStr.indexOf("]") != -1){ jsonStr = jsonStr.replace("]", ""); } JSONObject obj = new JSONObject().fromObject(jsonStr);//將json字符串轉換爲json對象 Person jb = (Person)JSONObject.toBean(obj,Person.class);//將建json對象轉換爲Person對象 return jb;//返回一個Person對象 } }
package baz.bean; public class Person { private String name; private int num; private String sex; private int age; public Person() { // TODO Auto-generated constructor stub } public Person(String name, int num, String sex, int age) { super(); this.name = name; this.num = num; this.sex = sex; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = 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() { return age; } public void setAge(int age) { this.age = age; } }
將java對象轉換爲json字符串
package baz.cons; import net.sf.json.JSONObject; /** * 將java對象轉換爲json字符串 * @author Administrator * */ public class ConsJson { public ConsJson() { // TODO Auto-generated constructor stub } public String Object2Json(Object obj){ JSONObject json = JSONObject.fromObject(obj);//將java對象轉換爲json對象 String str = json.toString();//將json對象轉換爲字符串 return str; } }
測試類:
package baz.test; import 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\",\"age\":24}"); pj.parse(); //將一個json字符串轉換爲java對象 Person p = pj.JSON2Object(); System.out.println("Name:" + p.getName()); System.out.println("Num:" + p.getNum()); System.out.println("Sex:" + p.getSex()); System.out.println("age:" + p.getAge()); //將一個java對象轉換爲Json字符串 Person p1 = new Person("gu1",123,"male",23); ConsJson cj = new ConsJson(); String str1 = cj.Object2Json(p1); System.out.println(str1); } }
測試輸出以下:
gu 123456 male 24 Name:gu Num:123456 Sex:male age:24 {"age":23,"name":"gu1","num":123,"sex":"male"}