JSONObject和JSONArray的JSON操作
JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式。它基于JavaScript(Standard ECMA-262 3rd Edition - December 1999)的一个子集。在一次项目中用到页面返回数据处理。
1、引入 json.jar 包;
2、JSONObject 对象使用;
首先创建 JSONObject 对象,使用 put 方法给对象添加元素。
package JSON;
import net.sf.json.JSONArray;
import net.sf.json.JSONException;
import net.sf.json.JSONObject;
public class JSONObjectSampl {
private static JSONObject createJSONObject(){
JSONObject jsonObj = new JSONObject();
try {
jsonObj.put("id", "1");
jsonObj.put("name", "sam");
jsonObj.put("email", "[email protected]");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return jsonObj;
}
public static void main(String[] args) {
//静态方法,直接通过类名.方法名调用
JSONObject jsonObj = JSONObjectSampl.createJSONObject();
System.out.println("jsonObj:"+jsonObj);
//判断输出对象类型
boolean isArray = jsonObj.isArray();
boolean isEmpty = jsonObj.isEmpty();
boolean isNullObject = jsonObj.isNullObject();
System.out.println("isArray:" + isArray + "\tisEmpty:" + isEmpty + "\tisNullObject:" + isNullObject);
//添加属性,继续在 jsonObj 后面追加元素
jsonObj.element("address", "广东深圳");
System.out.println("添加后的属性元素:"+jsonObj);
//返回 jsonArray 对象
JSONArray jsonArray = new JSONArray();
jsonArray.add(0, "this is a jsonArray value");
jsonArray.add(1,"another jsonArray value");
jsonObj.element("jsonArray", jsonArray);
//获取jsonArray
JSONArray array = jsonObj.getJSONArray("jsonArray");
System.out.println("jsonObj"+jsonObj);
System.out.println("返回一个jsonArray 对象"+array);
//根据key 返回一个字符串
String name = jsonObj.getString("name");
System.out.println("name"+name);
//把字符转换为 JSONObj
String temp = jsonObj.toString();
JSONObject object = JSONObject.fromObject(temp);
System.out.println("temp:"+object.get("address"));
}
}3、JSONArrayList 对象使用
使用 element 方法保存对象
package JSON;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
public class JSONTest {
public static void main(String[] agrs){
JSONObject jsonObj0 = new JSONObject();
JSONObject jsonObj1 = new JSONObject();
JSONObject jsonObj2 = new JSONObject();
JSONObject jsonObj3 = new JSONObject();
JSONArray jsonArray = new JSONArray();
jsonObj0.put("name0", "张三");
jsonObj0.put("sex0", "female0");
System.out.println("jsonObj0:\t"+jsonObj0);
jsonObj1.put("nam1", "李四");
jsonObj1.put("sex1", "male1");
System.out.println("jsonObj1:\t"+jsonObj1);
jsonObj2.put("item0", jsonObj0);
jsonObj2.put("item1", jsonObj1);
System.out.println("jsonObj2:\t"+jsonObj2);
jsonObj3.element("jsonObj3", jsonObj2);
System.out.println("jsonObj3:\t"+jsonObj3);
//往JSONArray中添加JSONObject对象。发现JSONArray跟JSONObject的区别就是JSONArray比JSONObject多中括号[]
jsonArray.add(jsonObj1);
System.out.println("jsonArray:"+jsonArray);
JSONObject jsonObj4 = new JSONObject();
jsonObj4.element("weather", jsonArray);
System.out.println("jsonObj4:"+jsonObj4);
}
}4、HTML页面处理
var dataObj=eval("("+data+")");//转换为json对象.
为什么要 eval这里要添加 “("("+data+")");//”呢?
原因在于:eval本身的问题。 由于json是以”{}”的方式来开始以及结束的,在JS中,它会被当成一个语句块 来处理,所以必须强制性的将它转换成一种表达式。
加上圆括号的目的是迫使eval函数在处理JavaScript代码的时候强制将括号内的表达式 (expression)转化为对象,而不是作为语句(statement)来执行。举一个例子,例如对象字面量{ },如若不加外层的括号,那么eval会将大括号识别为JavaScript代码块的开始和结束标记,那么{} 将会被认为是执行了一句空语句。所以下面两个执行结果是不同的:
alert(eval("{}"); // return undefined
alert(eval("({})");// return object[Object]
5、http://json-lib.sourceforge.net/apidocs/jdk15/index.html
6、 JSON.JAR 下载地址
http://download.csdn.net/detail/leiyinsu/7064451