一、json-lib示例
package net.pink.test;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
public class Main2 {
/**
* @param args
*/
public static void main(String[] args) {
// object to json
// jsonObject
Map<String, Object> map = new HashMap<String, Object>();
User user = new User("pink", 34);
User user2 = new User("pink2", 44);
User user3 = new User("pink3", 22);
map.put("user", user);
map.put("user2", user2);
map.put("user3", user3);
JSONObject jObject = JSONObject.fromObject(map);
// jObject.toString() json字符串
System.out.println(jObject.toString());
// jsonArray
List<String> list = new ArrayList<String>();
list.add("php");
list.add("java");
list.add("c++");
list.add("perl");
list.add("python");
JSONArray jsArray = JSONArray.fromObject(list);
// jsArray.toString() json字符串
System.out.println(jsArray.toString());
// json to object
// jsonObject
JSONObject jObject2 = JSONObject.fromObject(jObject.toString());
jObject2.getJSONObject("user");
JSONObject jObject3 = jObject2.getJSONObject("user");
String username = jObject3.getString("username");
System.out.println(username);
// jsonArray
JSONArray jsArray2 = JSONArray.fromObject(jsArray.toArray());
String lang = jsArray2.getString(1);
System.out.println(lang);
}
public static class User {
private String username;
private int age;
public User(String username, int age) {
super();
this.username = username;
this.age = age;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
}
}
二、org.json示例
package net.pink.test;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
// object to json
// jsonObject
Map<String, Object> map = new HashMap<String, Object>();
User user = new User("pink", 34);
User user2 = new User("pink2", 44);
User user3 = new User("pink3", 22);
map.put("user", user);
map.put("user2", user2);
map.put("user3", user3);
JSONObject jObject = new JSONObject(map);
System.out.println(jObject);
// josnArray
List<String> list = new ArrayList<String>();
list.add("php");
list.add("java");
list.add("c++");
list.add("perl");
list.add("python");
JSONArray jsArray = new JSONArray(list);
System.out.println(jsArray);
// json to object
try {
// jsonObject
JSONObject jObject2 = new JSONObject(jObject.toString());
JSONObject jObject3 = jObject2.getJSONObject("user");
String username = jObject3.getString("username");
System.out.println(username);
// jsonArray
JSONArray jsArray2 = new JSONArray(jsArray.toString());
String lang = jsArray2.getString(1);
System.out.println(lang);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static class User {
private String username;
private int age;
public User(String username, int age) {
super();
this.username = username;
this.age = age;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
}
}