Android中json数据手动解析方式

来源:互联网 发布:淘宝产品拍摄技巧 编辑:程序博客网 时间:2024/06/06 09:29
json数据手动解析:
每个大括号都是一个JSONObject
中括号为集合(JSONArray

代码示例:
一段json数据:
{
    "reason": "成功的返回",
    "result": {
        "stat": "1",
        "data": [
            {
                "uniquekey": "6de190ba8b7e354806c9d827c6e0d6f7",
                "title": "美容业乱象调查:女子1万整形被整残 花百万修复",
                "date": "2017-03-22 13:29",
                "category": "头条",
                "author_name": "齐鲁网",
                "url": "http://mini.eastday.com/mobile/170322132951431.html",
                "thumbnail_pic_s": "http://00.imgmini.eastday.com/mobile/20170322/20170322132951_98974bebe08b1afb4bd56c4834fef517_1_mwpm_03200403.jpeg",
                "thumbnail_pic_s02": "http://00.imgmini.eastday.com/mobile/20170322/20170322132951_98974bebe08b1afb4bd56c4834fef517_2_mwpm_03200403.jpeg",
                "thumbnail_pic_s03": "http://00.imgmini.eastday.com/mobile/20170322/20170322132951_98974bebe08b1afb4bd56c4834fef517_3_mwpm_03200403.jpeg"
            },
            {
                "uniquekey": "a3106bde1776232b407a355084bce36a",
                "title": "融合探索闯\u201c江湖\u201d",
                "date": "2017-03-22 14:16",
                "category": "头条",
                "author_name": "人民日报中央厨房",
                "url": "http://mini.eastday.com/mobile/170322141636146.html",
                "thumbnail_pic_s": "http://09.imgmini.eastday.com/mobile/20170322/20170322141636_1673587ab3b1064924d0243610e73f64_1_mwpm_03200403.jpeg"
            }]

解析步骤:
1.遇到大括号就getJSONObject
2.遇到中括号就getJSONArray,想获取JSONArray集合中的元素,就给下标就行,下标从零开始计算。

//sjson数据字符串类型(StringJSONObject jsonObject = new JSONObject(s);JSONObject result = jsonObject.getJSONObject("result");JSONArray data = result.getJSONArray("data");JSONObject jsonObject1 = data.getJSONObject(2);String uniquekey = jsonObject1.getString("uniquekey");String title = jsonObject1.getString("title");String date = jsonObject1.getString("date");String category = jsonObject1.getString("category");String author_name = jsonObject1.getString("author_name");String url1 = jsonObject1.getString("url");String thumbnail_pic_s = jsonObject1.getString("thumbnail_pic_s");




0 0
原创粉丝点击