【问题标题】:How to create Parcelable class using following JSON Data?如何使用以下 JSON 数据创建 Parcelable 类?
【发布时间】:2017-04-18 12:55:20
【问题描述】:

每次调用api都会改变数组键,所以如何制作parcel类。

 {
        "status": 200,
        "message": "Ground Schedules",
        "data": {
            "Schedules": {
                "2017-05-04": [
                    {
                        "id": "1216",
                        "ground_id": "100",
                        "ground_court_id": "71",
                        "start_time": "02:00:00",
                        "end_time": "04:00:00",
                        "price": "100.00",
                        "is_available": "1"
                    },
                    {
                        "id": "1258",
                        "ground_id": "100",
                        "ground_court_id": "71",
                        "start_time": "02:00:00",
                        "end_time": "04:00:00",
                        "price": "100.00",
                        "is_available": "1"
                    },
                    {
                        "id": "1259",
                        "ground_id": "100",
                        "ground_court_id": "71",
                        "start_time": "04:00:00",
                        "end_time": "06:00:00",
                        "price": "100.00",
                        "is_available": "1"
                    },
                    {
                        "id": "1215",
                        "ground_id": "100",
                        "ground_court_id": "71",
                        "start_time": "24:00:00",
                        "end_time": "02:00:00",
                        "price": "100.00",
                        "is_available": "1"
                    }
                ],
                "2017-05-05": [
                    {
                        "id": "1266",
                        "ground_id": "100",
                        "ground_court_id": "71",
                        "start_time": "03:00:00",
                        "end_time": "04:00:00",
                        "price": "100.00",
                        "is_available": "1"
                    }
                ],
                "2017-05-06": [
                    {
                        "id": "1268",
                        "ground_id": "100",
                        "ground_court_id": "71",
                        "start_time": "01:00:00",
                        "end_time": "02:00:00",
                        "price": "100.00",
                        "is_available": "1"
                    },
                    {
                        "id": "1267",
                        "ground_id": "100",
                        "ground_court_id": "71",
                        "start_time": "24:00:00",
                        "end_time": "01:00:00",
                        "price": "100.00",
                        "is_available": "1"
                    }
                ]
            },
            "GroundBookedSlots": [
                {
                    "id": "1120",
                    "ground_id": "100",
                    "ground_court_id": "71",
                    "start_time": "17:00:00",
                    "end_time": "18:00:00",
                    "price": "500.00",
                    "date": "2017-04-19"
                },
                {
                    "id": "1112",
                    "ground_id": "100",
                    "ground_court_id": "71",
                    "start_time": "17:00:00",
                    "end_time": "18:00:00",
                    "price": "500.00",
                    "date": "2017-04-18"
                }
            ]
        }
    }

【问题讨论】:

    标签: android json parcelable parcel


    【解决方案1】:

    您的 Json 数组是动态的。使用 Iterator

    要使用迭代器,请按照以下步骤操作 -

    1. 通过调用 集合的 iterator() 方法。
    2. 设置一个循环来调用 hasNext( )。让循环迭代 只要 hasNext( ) 返回 true。
    3. 在循环中,通过调用 next() 获取每个元素。

    首先,获取关键值

    final JSONObject getJson = JsonObject.getJSONObject("Schedules");
                    Iterator  iteratorObj = getJson.keys();
                    ArrayList<String> al_getAllArray=new ArrayList<String>();
                    while (iteratorObj.hasNext())
                    {
                        String getJsonArray = (String)iteratorObj.next();
                        System.out.println("Key: " + Key + "------>" + getJsonArray );
                        al_getAllArray.add(getJsonArray);
                        .....//do your work//.......
    
                    }
    

    【讨论】:

    • 谢谢,但我想创建 Parcelable 类,就像这个公共类 Schedules implements Parcelable { }
    • @ckpatel 我知道Parcelable 。我可以知道为什么这种情况需要这样做吗??
    • 我在现有应用程序和这个使用过的可打包类中工作,所以我对此一无所知
    • 你能帮帮我吗
    • @ckpatel 首先告诉我,首先需要Json动态数组元素吗??
    猜你喜欢
    • 1970-01-01
    • 2019-02-27
    • 1970-01-01
    • 2018-06-13
    • 1970-01-01
    • 2021-12-17
    • 2011-09-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多