package ivyy.taobao.com.domain.orgjson; import java.util.ArrayList; import java.util.List; import ivyy.taobao.com.entity.Address; import ivyy.taobao.com.entity.Student; import net.sf.json.JSONFunction; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import org.json.JSONStringer; import org.json.JSONTokener; import org.json.JSONUtil; /** *@DEMO:json *@Java:OrgJson.java *@Date:2015-1-19下午12:28:31 *@Author:龙叔 *@Email:jilongliang@sina.com *@Weibo:http://weibo.com/jilongliang *@Version:1.0 *@Description: *org.json字符串转成JSON *org.json字符串转换成对象 *org.json字符串转成List数组泛型 *org.json的JSONStringer的使用 *org.json的JSONTokener使用 */ @SuppressWarnings("all") public class OrgJson { public static void main(String[] args)throws Exception { //String2Json(); //String2Object(); //String2List1(); //String2List2(); //JSONFunction(); String json=JSONStringer(); System.out.println(json); } /*** * org.json字符串转成JSON * @throws Exception */ private static void String2Json()throws Exception{ String jsonStr="{\"age\":190,\"sex\":\"男\",\"userName\":\"周伯通\"}"; //转换成JSONObject数据 JSONObject jsonObj=new JSONObject(jsonStr); Integer age=jsonObj.getInt("age");//获取age String sex=jsonObj.getString("sex");//获取sex String userName=jsonObj.get("userName").toString();//获取userName System.out.println(userName); System.out.println(sex); System.out.println(age); } /*** * org.json字符串转换成对象 * JSONUtil * @throws Exception */ private static void String2Object()throws Exception{ //String jsonStr="{\"age\":190,\"userName\":\"周伯通\",\"birthday\":\"1888-1-1\",\"address\":\"洛阳\",\"email\":\"zhoubotong@sina.com\"}"; StringBuffer buff=new StringBuffer(); buff.append("{"); buff.append("\"age\":").append("198").append(","); buff.append("\"userName\":").append("\"周伯通\"").append(","); buff.append("\"sex\":").append("\"男\"").append(","); buff.append("\"birthday\":").append("\"1888-1-1\"").append(","); buff.append("\"address\":").append("\"洛阳\"").append(","); buff.append("\"email\":").append("\"zhoubotong@sina.com\""); buff.append("}"); String jsonStr=buff.toString(); Student st=(Student)JSONUtil.Json2Bean(jsonStr,Student.class); System.out.println(st.getUserName()); System.out.println(st.getAddress()); } /*** * org.json字符串转成List数组泛型 * @throws Exception */ private static void String2List1()throws Exception{ StringBuffer buff=new StringBuffer(); //String jsonStr="{\"age\":190,\"email\":\"zhoubotong@sina.com\"}"; buff.append("["); buff.append("{"); buff.append("\"age\":").append("198").append(","); buff.append("\"userName\":").append("\"周伯通\"").append(","); buff.append("\"sex\":").append("\"男\"").append(","); buff.append("\"birthday\":").append("\"1888-1-1\"").append(","); buff.append("\"address\":").append("\"洛阳\"").append(","); buff.append("\"email\":").append("\"zhoubotong@sina.com\""); buff.append("},"); buff.append("{"); buff.append("\"age\":").append("198").append(","); buff.append("\"userName\":").append("\"郭靖\"").append(","); buff.append("\"email\":").append("\"guojing@sina.com\""); buff.append("}"); buff.append("]"); String jsonStr=buff.toString(); //转换成JSONArray数据 JSONArray jarr=new JSONArray(jsonStr); for (int i = 0; i < jarr.length(); i++) { JSONObject jsonObj=(org.json.JSONObject) jarr.get(i); Integer age=jsonObj.getInt("age");//获取age String sex=jsonObj.getString("sex");//获取sex String userName=jsonObj.get("userName").toString();//获取userName System.out.println(userName); System.out.println(sex); System.out.println(age); } } /*** * org.json字符串转成List数组泛型 * @throws Exception */ private static void String2List2()throws Exception{ StringBuffer buff=new StringBuffer(); //String jsonStr="{\"age\":190,"); buff.append("\"email\":").append("\"guojing@sina.com\""); buff.append("}"); buff.append("]"); String jsonStr=buff.toString(); //转换成JSONArray数据 JSONArray jarr=new JSONArray(jsonStr); for (int i = 0; i < jarr.length(); i++) { JSONObject jsonObj=(JSONObject) jarr.get(i);//获取JSONObject //转换成Student对象 Student stu=(Student)JSONUtil.Json2Bean(jsonObj.toString(),Student.class); System.out.println(stu.getUserName()); System.out.println(stu.getSex()); System.out.println(stu.getAge()); System.out.println(stu.getEmail()); System.out.println(stu.getAddress()); } } /** * org.json的JSONStringer的使用 * @return */ private static String JSONStringer(){ JSONStringer jsonStringer = new JSONStringer(); try { jsonStringer.object(); jsonStringer.key("userName"); jsonStringer.value("周伯通"); jsonStringer.key("id"); jsonStringer.value(20150119); jsonStringer.key("email"); jsonStringer.value("zhoubotong@sina.com"); jsonStringer.endObject(); } catch (JSONException e) { e.printStackTrace(); } return jsonStringer.toString(); } /*** * org.json的JSONTokener使用 */ private static void JSONTokener() { String jsonStr = "{\"age\":190,\"userName\":\"周伯通\"}"; JSONTokener jsonTokener = new JSONTokener(jsonStr); JSONObject jsonObj=null; String userName = "",sex=""; Integer age=null; try { jsonObj = (JSONObject) jsonTokener.nextValue(); userName = jsonObj.getString("userName"); age = jsonObj.getInt("age"); sex = jsonObj.getString("sex"); System.out.println(userName+"\t"+age+"\t"+sex); } catch (JSONException e) { e.printStackTrace(); } } /*** * 通过json-lib的JSONFunction结合org.json的JSONArray一起使用 * @throws JSONException */ public static void JSONFunction() throws JSONException { //1、创建对象List List list = new ArrayList(); list.add(new Integer(1)); list.add(new Boolean(true)); list.add(new Character('布')); list.add(new char[] { '东','升','布','艺' }); list.add(null); list.add("json"); list.add(new String[] { "东升","布","艺" }); list.add(new JSONFunction(new String[] { "i" },"alert(i)")); list.add(new Address("中国","广东省",1988,"527526","闻莺路")); System.out.println(list); //2、list转JSONArray JSONArray jsArr = new JSONArray(list); /* * 注:org.json不支持list中非JSON对象与数组对象外的对象,会直接调用对象的toString方法 */ System.out.println(jsArr.toString(4)); //3、从JSON串到JSONArray JSONArray jsArr1 = new JSONArray(jsArr.toString()); //4、--从JSONArray里读取 //print: json System.out.println(((JSONArray) jsArr1.get(6)).get(0)); //5、不能正确读取对象的信息 print: address.city = bean.Address@1cf8583 System.out.println("address.city = " + jsArr1.get(8)); System.out.println("\n"); } }
package ivyy.taobao.com.entity; import java.io.Serializable; import com.google.gson.annotations.Expose; /** * Gson gson=new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create(); * 创建Gson对象,没有@Expose注释的属性将不会被序列化 * serialize序列化 默认 true * deserialize反序列化 默认 true * @author liangjilong * @Email:jilongliang@sina.com */ public class Address implements Serializable{ @Expose(serialize=false)// 序列化 /Address类已经序列化 private int id; @Expose(deserialize=false)// 反序列化 private String country;//国家 private String province;//省份 private String city;//城市 private String street;//街道 private String district;//地区 private String cityCode;//邮政编码 private String streetNumber;//街道号 private String weibo;//微博 public Address() { } public Address(String country,String province,int id,String cityCode,String streetNumber) { System.out.println(country+"\t"+province+"\t"+id+cityCode+"\t"+streetNumber); } public Address(int id,String country) { this.id=id; this.country=country; } public String getCityCode() { return cityCode; } public void setCityCode(String cityCode) { this.cityCode = cityCode; } public String getStreetNumber() { return streetNumber; } public void setStreetNumber(String streetNumber) { this.streetNumber = streetNumber; } public String getStreet() { return street; } public void setStreet(String street) { this.street = street; } public String getDistrict() { return district; } public void setDistrict(String district) { this.district = district; } public String getCity() { return city; } public void setCity(String city) { this.city = city; } public String getProvince() { return province; } public void setProvince(String province) { this.province = province; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getCountry() { return country; } public void setCountry(String country) { this.country = country; } public String getWeibo() { return weibo; } public void setWeibo(String weibo) { this.weibo = weibo; } }
package ivyy.taobao.com.entity; import java.io.Serializable; /** *@Author:liangjl *@Date:2014-12-19 *@Version:1.0 *@Description: */ public class Student implements Serializable{ private Integer age; private String sex; private String userName; private String birthday; private String address; private String email; public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getBirthday() { return birthday; } public void setBirthday(String birthday) { this.birthday = birthday; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } }