GSON:
Gson是google开发的json格式解析包,其特色是在解析json以前必须知道所传输的json数据格式,并定义一系列层次结构与json层次结构相同的类。换句话说,若是传输的json结构为:java
{ "name":"relin", "sex":"male", "age":26 }
那么,就必须预先定义一个成员变量名字与json中属性名字彻底相同的类:json
class Person { public String name; public String sex; public int age; }
Gson解析json有三个特色:数组
- 若是预先定义的类中不包含json中的某个属性,则该属性就不会被解析出来,可是其余成员仍然能正常解析
- 命名必须彻底相同,不然不会被正常解析
- 类的成员变量能够是public,也能够是private
让咱们来看两个简单的解析与反解析过程:ide
1. 定义类:this
package com.relin.gson.data; public class Person { private String name; private int age; private int sex; /** * @return the name */ public String getName() { return name+"*****"; } /** * @param name the name to set */ public void setName(String name) { this.name = name; } /** * @return the age */ public int getAge() { return age; } /** * @param age the age to set */ public void setAge(int age) { this.age = age; } @Override public String toString() { return name + ":" + age; } }
2. String to json:google
private static boolean StringToJson(){ try{ String str = "{\"name\":\"name0\",\"age\":0}"; Gson gson = new Gson(); Person person= gson.fromJson(str, Person.class); System.out.println(person); } catch (Exception e){ return false; } return true; }
3. Json to String:spa
private static boolean JsonToString(){ try{ Gson gson = new Gson(); ArrayList<Person> persons = new ArrayList<Person>(); for (int i = 0; i < 10; i++) { Person p = new Person(); p.setName("name" + i); p.setAge(i * 5); persons.add(p); } String str = gson.toJson(persons); System.out.println(str); } catch (Exception e){ return false; } return true; }
4. 调用能够以下所示:code
package com.relin.gson; import java.util.ArrayList; import com.google.gson.Gson; import com.relin.gson.data.Person; import com.relin.gson.data.UrlResponse; public class Example { private static boolean JsonToString(){ try{ Gson gson = new Gson(); ArrayList<Person> persons = new ArrayList<Person>(); for (int i = 0; i < 10; i++) { Person p = new Person(); p.setName("name" + i); p.setAge(i * 5); persons.add(p); } String str = gson.toJson(persons); System.out.println(str); } catch (Exception e){ return false; } return true; } private static boolean StringToJson(){ try{ String str = "{\"name\":\"name0\",\"age\":0}"; Gson gson = new Gson(); Person person= gson.fromJson(str, Person.class); System.out.println(person); } catch (Exception e){ return false; } return true; } public static void main(String agrs[]){ StringToJson(); JsonToString() } }
JSONObject 是一个final类,继承了Object,实现了JSON接口对象
构造方法以下:继承
JSONObject();建立一个空的JSONObject对象
JSONObject(boolean isNull);建立一个是否为空的JSONObject对象
普通方法以下:
fromBean(Object bean);静态方法,经过一个pojo对象建立一个JSONObject对象
fromJSONObject(JSONObject object);静态方法,经过另一个JSONObject对象构造一个JSONObject对象
fromJSONString(JSONString string);静态方法,经过一个JSONString建立一个JSONObject对象
toString();把JSONObject对象转换为json格式的字符串
iterator();返回一个Iterator对象来遍历元素
其中包含有个主要的类:
1.JSONObject至关与json中的字典类型
2.JSONArray至关与json中的数组类型
基本用法以下:
import net.sf.json.JSONArray; import net.sf.json.JSONObject; public class JSONObjectSample { //建立JSONObject对象 private static JSONObject createJSONObject(){ JSONObject jsonObject = new JSONObject(); jsonObject.put("name", "kevin"); jsonObject.put("Max.score", new Integer(100)); jsonObject.put("Min.score", new Integer(50)); jsonObject.put("nickname", "picglet"); return jsonObject; } public static void main(String[] args) { JSONObject jsonObject = JSONObjectSample.createJSONObject(); //输出jsonobject对象 System.out.println("jsonObject==>"+jsonObject); //判读输出对象的类型 boolean isArray = jsonObject.isArray(); boolean isEmpty = jsonObject.isEmpty(); boolean isNullObject = jsonObject.isNullObject(); System.out.println("isArray:"+isArray+" isEmpty:"+isEmpty+" isNullObject:"+isNullObject); //添加属性 jsonObject.element("address", "swap lake"); System.out.println("添加属性后的对象==>"+jsonObject); //返回一个JSONArray对象 JSONArray jsonArray = new JSONArray(); jsonArray.add(0, "this is a jsonArray value"); jsonArray.add(1,"another jsonArray value"); jsonObject.element("jsonArray", jsonArray); JSONArray array = jsonObject.getJSONArray("jsonArray"); System.out.println("返回一个JSONArray对象:"+array); //添加JSONArray后的值 //{"name":"kevin","Max.score":100,"Min.score":50,"nickname":"picglet","address":"swap lake", //"jsonArray":["this is a jsonArray value","another jsonArray value"]} System.out.println(jsonObject); //根据key返回一个字符串 String jsonString = jsonObject.getString("name"); System.out.println("jsonString==>"+jsonString); //解析一个json对象(能够解析不一样类型的数据) jsonObject = getJSONObject("{d:test,e:true,b:1.1,c:1,a:1}"); System.out.println(jsonObject); //{"d":"test","e":true,"b":1.1,"c":1,"a":1} System.out.println(jsonObject.getInt("a")); System.out.println(jsonObject.getDouble("b")); System.out.println(jsonObject.getLong("c")); System.out.println(jsonObject.getString("d")); System.out.println(jsonObject.getBoolean("e")); } public static JSONObject getJSONObject(String str) { if (str == null || str.trim().length() == 0) { return null; } JSONObject jsonObject = null; try { jsonObject = new JSONObject(str); } catch (JSONException e) { e.printStackTrace(System.err); } return jsonObject; } }