1.JSON对象是一个无序的" ‘ 名称/值‘ 对 " 集合。一个对象以"{" (左括号)开始,"}" (右括号)结束。每个"名称" 后跟一个":"(冒号);" ‘名称/值 ‘ 对之间使用","(逗号)分隔.
列如:{
"name": "jackson",
"age":100
}
数组是值(value)的有序集合。一个数组以"["(左中括号)开始,"]"(右中括号)结束。值之间使用","(逗号)分隔。
如:{
"students":
[
{"name":"jackson","age":100},
{"name":"liming","age":80},
]
}
2.将Object对象通过JsonObject转化成json的字符串。
/**
* @param key
* 表示json字符串的头信息
* @param object
* 是对解析的集合的类型
* @return
*/
JSONObject jsonObject = new JSONObject(); //定义Json对象
jsonObject.put(key, value);
jsonObject.toString(); //将对象转化成json字符串
3.将json字符串通过JsonObject转化成字符串,一般是从服务端提起数据到客户端。分以下几种情况:
注意: public class Person {
private int id;
private String name;
private String address;
...
}
(1) 当object为person对象时,其对应的json字符串是:{"person":{"address":"beijing","id":1001,"name":"jack"}},则解析json字符串方法如下:
Person person = new Person();
JSONObject jsonObject = new JSONObject(jsonString); //通过Json字符串定义一个JsonObject对象。 JSONObject personObject = jsonObject.getJSONObject("person");
person.setId(personObject.getInt("id"));
person.setName(personObject.getString("name"));
person.setAddress(personObject.getString("address"));
(2) 当object为List<Person>对象时,其对应的json字符串是:{"persons":[{"address":"guangxi","id":1001,"name":"jack"},{"address":"guangdong","id":1002,"name":"rose"}]},则解析json字符串方法如下:
List<Person> list = new ArrayList<Person>();
JSONObject jsonObject = new JSONObject(jsonString);
// 返回json的数组
JSONArray jsonArray = jsonObject.getJSONArray(key);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject2 = jsonArray.getJSONObject(i); // JSONObject getJSONArray(int index)
Person person = new Person(); //Returns the value at index
if it exists and is a JSONArray @index为数组索引
person.setId(jsonObject2.getInt("id"));
person.setName(jsonObject2.getString("name"));
person.setAddress(jsonObject2.getString("address"));
list.add(person);
(3) 当object为List<String> 对象时,其对应的json字符串是:{"liststring":["beijing","shanghai","hunan"]},则解析json字符串方法如下:
List<String> list = new ArrayList<String>();
JSONObject jsonObject = new JSONObject(jsonString);
JSONArray jsonArray = jsonObject.getJSONArray(key);
for (int i = 0; i < jsonArray.length(); i++) {
String msg = jsonArray.getString(i); //String getString(int index)
list.add(msg); //Returns the value at index
if it exists, coercing it if necessary.
}
(4) 当object为person对象时,其对应的json字符串是:{"listmap":[{"address":"beijing","name":"jack","id":1001},{"address":"shanghai","name":"rose","id":1002}]},则解析json字符串方法如下:
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
JSONObject jsonObject = new JSONObject(jsonString);
JSONArray jsonArray = jsonObject.getJSONArray(key);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject2 = jsonArray.getJSONObject(i);
Map<String, Object> map = new HashMap<String, Object>();
Iterator<String> iterator = jsonObject2.keys(); //
Iterator | keys()
Returns an iterator of the |
while (iterator.hasNext()) {
String json_key = iterator.next();
Object json_value = jsonObject2.get(json_key);
if (json_value == null) {
json_value = "";
}
map.put(json_key, json_value);
}
list.add(map);
json技术讲解完毕!!