JSON
Android 开发中网络数据一般是采用了 JSON 的方式进行传输,所以 JSON 数据解析是很重要的一件事情。
JSON 数据是以键值对的形式存储的,key:value,碰到数据多的时候自然就会用到了数组。
Type_One
简单的以直接 key:value 直接表现出来
{
"text": "© 桃子",
"img": "https://pic3.zhimg.com/v2-beb690851729623283314060e56bb506_xxdpi.jpg"
}
Type_Two
把多个 key:value 的组合看成一个数组元素,然后使用 [ ] 包含全部的数组元素。
{
"error": false,
"results": [
{
"_id": "5872f7f2421aa9316407fb84",
"createdAt": "2017-01-09T10:39:46.599Z",
"desc": "1-9",
"publishedAt": "2017-01-09T11:46:59.821Z",
"source": "chrome",
"type": "福利",
"url": "http://ww1.sinaimg.cn/large/610dc034gw1fbk6h23k3ij20u00jymyw.jpg",
"used": true,
"who": "daunahu"
},
{
"_id": "584dffdd421aa963eaaee172",
"createdAt": "2016-12-12T09:39:41.294Z",
"desc": "12-12",
"publishedAt": "2016-12-12T11:30:54.254Z",
"source": "chrome",
"type": "福利",
"url": "http://ww4.sinaimg.cn/large/610dc034jw1fanrdyaxi6j20u00k1ta9.jpg",
"used": true,
"who": "daimajia"
}……
]
}
JSON 解析
对于 Type_One 的数据类型,我们可以通过以下方式进行数据解析,可以看到,其中传入了一个 String (字符串) 类型的 s,这个表示从网络拿到的原始信息(字符串)
private void JsonParse(String s) {
try {
JSONObject jsonObject = new JSONObject(s);
String text = jsonObject.getString("text");
String img = jsonObject.getString("img");
} catch (JSONException e) {
e.printStackTrace();
}
}
对于 Type_Two 的数据类型,就稍微有些麻烦了(也就是多那么几行代码的事情),因为其中还包含了一个数组,我们需要通过数组的 key
把数组对象获到,然后遍历数组元素,并取出所需要的值然后存在统一的数据集合中。
imgList 是我在外面定义的全局变量是一个 list 集合,List imgList=new ArrayList<>();
private void JsonArrayParse(String json) {
try {
JSONObject jsonObject = new JSONObject(json);
JSONArray meizi = jsonObject.optJSONArray("results");
for (int i = 0; i < meizi.length(); i++) {
JSONObject itemObject = meizi.optJSONObject(i);
String imgUrl = itemObject.optString("url");
imgList.add(i, imgUrl);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
找不同
如果仔细看上面的两个类型的JSON解析的代码,就会发现,有些不同 itemObject.optString(“url”); 和jsonObject.getString(“img”);,这两个同样是通过 JSONObject 去获取数据,但是,使用的方法却不同,原因何在?
/**
* Returns the value mapped by {@code name} if it exists, coercing it if
* necessary, or throws if no such mapping exists.
*
* @throws JSONException if no such mapping exists.
*/
public String getString(String name) throws JSONException {
Object object = get(name);
String result = JSON.toString(object);
if (result == null) {
throw JSON.typeMismatch(name, object, "String");
}
return result;
}
上面这个是 JSON 解析中 getString 的源码,可以看到,在内部有一个判断,判断 result 是否为空,为空的时候,就抛出一个异常,然后我们在调用的地方就需要加一个 catch ,去捕获异常。
/**
* Returns the value mapped by {@code name} if it exists, coercing it if
* necessary, or the empty string if no such mapping exists.
*/
public String optString(String name) {
return optString(name, "");
}
/**
* Returns the value mapped by {@code name} if it exists, coercing it if
* necessary, or {@code fallback} if no such mapping exists.
*/
public String optString(String name, String fallback) {
Object object = opt(name);
String result = JSON.toString(object);
return result != null ? result : fallback;
}
上面这个是 JSON 解析中 optString 的源码,根据传入参数类型、个数的不同,现实了重载。我们在外部调用 itemObject.optString(“url”);,系统会再一次调用 optString(String name,String fallback) , 而后者就是进行了容错判断,即使你拿到的 JSON 数据中与 key 相对应的数据是为空的,系统就会返回一个默认值,这个默认值就是空的,但不是 null 。既然系统在后面还是会执行一次 optString(String name,String fallback) 的重载方法,那么我们也是可以直接调用 optString(String name,String fallback) 的,这样的话,不仅省去了使用 optString(String name) 的转换操作,而且还能自定义默认值,何乐而不为呢。