一,使用json.org (需要导入json-org.jar)
1,将json的字符串转换成JSONObject: 在使用api将json字符串转换成jsonObject对象之前首先应该保证json字符串的格式正确,可以使用http://json.parser.online.fr/来验证字符串的格式是否正确:
如果格式正确,则可以使用api进行json字符串到JSONObject对象的转化:
<pre name="code" class="java"><span style="white-space:pre"> </span><pre name="code" class="java">public class JsonOrgTestOne {
//将字符串解析成json对象
public static void JsonObjectTest(String jsonStr){
try {
JSONObject jsonObject=new JSONObject(jsonStr);
JSONObject result=jsonObject.getJSONObject("result");
//输出result对象
System.out.println("result:"+result); //{"datas":[{"name3":"3","name1":"1","name2":"2"},{"name3":"6","name1":"4","name2":"5"}],"tableName":"tableOne"}
//得到result对象的datas
JSONArray datas=result.getJSONArray("datas");
//遍历jsonArray:
for(int i=0;i<datas.length();i++){
//jsonArray中的每个元素都可以看成一个JSONObject
JSONObject item=datas.getJSONObject(i);
System.out.println("name1:"+item.getString("name1"));
System.out.println("name2:"+item.getString("name2"));
System.out.println("name3:"+item.getString("name3"));
}
//直接得到result对象中的jsonObject
System.out.println("tableName:"+result.getString("tableName"));
} catch (JSONException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
String jsonStr="{\"result\":{"+
"\"tableName\":\"tableOne\","+
"\"datas\":[{\"name1\":\"1\",\"name2\":\"2\",\"name3\":\"3\"},"+
"{\"name1\":\"4\",\"name2\":\"5\",\"name3\":\"6\"}]"+
"}"+
"}";
JsonOrgTestOne.JsonObjectTest(jsonStr);
}
}
2,当然也可以直接使用JSONArray来操作:
public class JsonOrgTestTwo {
public static void getJsonArrayFromStr(String jsonStr){
try {
//从字符串中得到jsonArray
JSONArray jsonArray=new JSONArray(jsonStr);
for(int i=0;i<jsonArray.length();i++){
JSONObject item=jsonArray.getJSONObject(i);
String seriesName=item.getString("seriesName");
String color=item.getString("color");
String results=item.getString("results");
System.out.println("serieName:"+seriesName+" color:"+color+" results:"+results);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
String jsonStr="["+
"{\"seriesName\":\"Model A12\",\"color\":\"A66EDD\",\"results\":[{\"2006\":\"35\"},{\"2007\":\"42\"},{\"2008\":\"31\"},{\"2009\":\"28\"},{\"2010\":\"34\"},{\"2011\":\"30\"}]}, "+
"{\"seriesName\":\"Model A12\",\"color\":\"A66EDD\",\"results\":[{\"2006\":\"35\"},{\"2007\":\"42\"},{\"2008\":\"31\"},{\"2009\":\"28\"},{\"2010\":\"34\"},{\"2011\":\"30\"}]}]";
JsonOrgTestTwo.getJsonArrayFromStr(jsonStr);
}
}
使用json.org可以很方便的将json字符串转换成jsonObject,但是当我们需要将Object对象转换成jsonObject的时候,使用json.org的new JSONObject(obj)得到的是{"class":"class xuri.json.lib.text.Student"} (class为key类的全名字符串为key)的JSONObject这样似乎还不能达到我们的要求,但是我们可以通过json-lib来很容易的做到:
二,使用json-lib 需要导入jar :json-lib-2.2.3-jdk15.jar ezmorph-1.0.3.jar commons-logging-1.1.3.jar commons-lang-2.3.jar commons-collections-3.2.1.jar commons-beanutils-1.7.0.jar
实体:
public class Student{
private String name;
private int age;
private boolean sex;
1,javaObject 到JSONObject测试
public class JsonLibTestOne {
public static void main(String[] args) {
Student stu=new Student("weijun_xiao",23,true);
//将学生对象转换成JSONObject
JSONObject jsonObject=JSONObject.fromObject(stu);
System.out.println("jsonObject:"+jsonObject);
//得到名称:
System.out.println("name:"+jsonObject.getString("name"));
}
}
2,json字符串转换成java对象:
public class JsonLibTestTwo {
public static void main(String[] args) {
String jsonStr="{\"name\":\"weijun_xiao\",\"age\":23,\"sex\":true}";
Student stu=(Student)JSONObject.toBean(JSONObject.fromObject(jsonStr), Student.class);
System.out.println("name:"+stu.getName());
}
}
3,List<Object>转换成JSONArray:
public class JsonLibTestThree {
public static void main(String[] args) {
List<Student> students=new ArrayList<Student>();
students.add(new Student("student1",23,true));
students.add(new Student("studnet2",23,false));
JSONArray jsonArray=JSONArray.fromObject(students);
System.out.println("jsonArray:"+jsonArray); //[{"age":23,"name":"student1","sex":true},{"age":23,"name":"studnet2","sex":false}]
}
}