android中如何解析没有Key的Json数据

新浪微博有一部分json数据如下,,你会发现这是个数组,但是数组没有key,数组中的对象也没有key。怎么解析呢?fastjson遇到这种无名数组套无名对象也会报异常。所以建议使用原生的json解析。

一、问题:

二、解析代码:package com.ht.weibo.weibomodel;import com.sina.weibo.sdk.openapi.models.Status;import org.json.JSONArray;import org.json.JSONException;import org.json.JSONObject;import java.util.ArrayList;/** * Created by annuo on 2015/6/6. */{private ArrayList<Status> statuses;public ArrayList<Status> getStatuses() {return statuses;}(ArrayList<Status> statuses) {this.statuses = statuses;}(String json) {if (json != null) {try {JSONArray array = new JSONArray(json);statuses = new ArrayList<>();for (int i = 0; i < array.length(); i++) {JSONObject jsonObject = array.getJSONObject(i);Status status = Status.parse(jsonObject);statuses.add(status);}} catch (JSONException e) {e.printStackTrace();}}}}

针对这个问题其他的解法,具体参考:(未实践)

?src=http%3A%2F%2Fsegmentfault.com%2Fq%2F1010000000174990&uid=0b4cd023903934907ec4a15ea96326f5&hid=29ae2ef764df9a92491d377a505210ed&pos=1&cid=9&time=1433608784138&from=click&restype=1&pagetype=0000004000000402&bu=web&query=json%E8%A

三、json数据的key值动态改变如何获取?

有个JSONObject.keys()的方法.返回值就是迭代器

Iterator<String> keys = json.keys();//因为只有一个对象,所以不必做更深入的迭代String key = keys.next();四、

json解析,如果某些字段或者对象是opt的,那就需要做一下空判断

版权声明:本文为博主原创文章,未经博主允许不得转载。

下午某时,天气晴,我在某地,想念你。

android中如何解析没有Key的Json数据

相关文章:

你感兴趣的文章:

标签云: