JSON
(JavaScript object notation ) 是一种轻量级的数据交换格式,它基于JavaScript。
它是一个“名称:值”这样形式的键值对的集合。
例如:
{
"username":"jack" , "age":"22"
}
{}代表一个对象,里面包含一个对象的一组或多组属性和属性值的键值对
[]代表一个数组,里面包含多个对象
[
{
"key1":"value1","key2":"value2"
}
,
{
"key1":"value1","key2":"value2"
}
]
var user = {"username":"jack" , "age":"22"} ;
alert(user.username);
var users = [{"username":"jack" , "age":"22"},{"username":"top" , "age":"22"}] ;
alert(users[0].username);
在java中如何使用json?
a:导入jar包
b:使用如下:
AAA a = new AAA();
a.setName("jack");
a.setAge(58);
a.setGender(‘男‘);
AAA aa = new AAA("mary",48,‘女‘);
AAA[] as = {a,aa} ;
// JSONObject jsonObject = JSONObject.fromObject(a);
// System.out.println(jsonObject.toString());
//{"age":58,"gender":"男","name":"jack"}
JSONArray jsonObject = JSONArray.fromObject(as);
System.out.println(jsonObject.toString());
//[{"age":58,"gender":"男","name":"jack"},{"age":48,"gender":"女","name":"mary"}]