对象格式: {“key”:obj,“key”:obj,“key”:obj}
数组格式: [obj,obj,obj]
可以互相嵌套
var json {
"param":[
{"firstname":"章","lastname":"子怡","age":100},
{"firstname":"流","lastname":"川枫","age":23}
]
};
alert(json.param[1].lastname);
json-lib工具
需要导包
对象Person转JSON
public void ObjectToJson(){
Person p = new Person("张山",29,"护士当家");
String json = JSONObject.fromObject(p).toString();
//此时的json是对象格式
}
数组转json
JSONArray
Gson工具
需要导包
对象集合转JSON
List<Person> list = new ArrayList<Person>();
list.add(new Person("张干",20,"护士"));
list.add(new Person("张萨",50,"院长"));
Gson gson = new Gson();
String json = gson.toJson(list);