android如何接受多个数组数据?

时间:2017-09-04 17:57:35

标签: php android arrays json laravel-5

新手在这里。我用laravel开发了我的后端,当我登录系统时它确实返回了我的结果。但是当这些数据传递给android时,我不知道如何处理它。

public static Expression<Func<TInput, object>> ToObjectExpression<TInput, TOutput>
                                       (this Expression<Func<TInput, TOutput>> expression)
{
    Expression converted = Expression.Convert(expression.Body, typeof(object));
    return Expression.Lambda<Func<TInput, object>>(converted, expression.Parameters);
}

这是我从服务器收到的数据的一些示例。我能够在Android应用程序中显示数据。这是我获取数据时的示例,以及我的服务器将如何处理它。

[
    {
        "id": 1,
        "name": "a",
        "email": "ddd@gmail.com",
        "address": null,
        "created_at": "2017-09-03 14:51:16",
        "updated_at": "2017-09-03 14:51:16",
        "items": [
            {
                "id": 1,
                "name": "test",
                "description": "test",                
                "pivot": {
                    "user_id": 1,
                    "items_id": 1,
                    "status": 1,
                    "created_at": "2017-09-03 14:51:48",
                    "quantity": 2,
                    "address": "gdhsjj",
                    "price": "2.0",
                    "updated_at": "2017-09-03 14:51:48"
                }
            },
            {
                "id": 1,
                "name": "test",
                "pivot": {
                    "user_id": 1,
                    "items_id": 1,
                    "status": 1,
                    "created_at": "2017-09-03 14:52:02",
                    "quantity": 1,
                    "address": "gdhsjj",
                    "price": "1.0",
                    "updated_at": "2017-09-03 14:52:02"
                }
            },
]
    },
    {
        "id": 2,
        "name": "dd",
        "email": "dd@gmail.com",
        "address": "123",
        "created_at": null,
        "updated_at": null,
        "items": []
    }
]

如果数据非常直接,这可以正常工作,但如果我想从 if (status.equals("SUCCESS")) { JSONArray jsonArray = json.getJSONArray("data"); JSONArray jsonArrayDetail = json.getJSONArray("detail"); ArrayList<PurchasedInfo> purchasedInfos = new ArrayList<PurchasedInfo>(); for (int i = 0; i < jsonArray.length(); i++) { JSONObject jsonObject = jsonArray.getJSONObject(i); JSONObject jsobObjectDetail = jsonArrayDetail.getJSONObject(i); PurchasedInfo purchasedInfo = new PurchasedInfo(); purchasedInfo.setI_id(jsobObjectDetail.getString("id")); purchasedInfo.setI_name(jsonObject.getString("name")); purchasedInfo.setI_price(jsonObject.getString("price")); purchasedInfo.setI_image(jsonObject.getString("image")); purchasedInfos.add(purchasedInfo); } } 获取数据,我不知道该怎么做。一切都需要先从服务器转换吗?或者我可以在获取数据时直接显示数据?

ps:我的问题有点乱,希望你们能理解或帮助我编辑我的问题。谢谢。如果有任何不清楚的地方,请告诉我。

2 个答案:

答案 0 :(得分:0)

您可以拥有另一个处理项目数据的类,并在Arraylist<ItemInfoClass> itemInfoClass上添加另一个PurchasedInfo。然后尝试使用此代码从json

中提取项目
 if (status.equals("SUCCESS")) {
                JSONArray jsonArray = json.getJSONArray("data");
                JSONArray jsonArrayDetail = json.getJSONArray("detail");
                ArrayList<PurchasedInfo> purchasedInfos = new ArrayList<PurchasedInfo>();
                for (int i = 0; i < jsonArray.length(); i++) {
                    JSONObject jsonObject = jsonArray.getJSONObject(i);
                    JSONObject jsobObjectDetail = jsonArrayDetail.getJSONObject(i);
                    PurchasedInfo purchasedInfo = new PurchasedInfo();
                    purchasedInfo.setI_id(jsobObjectDetail.getString("id"));
                    purchasedInfo.setI_name(jsonObject.getString("name"));


                    JSONArray itemArray = new JSONArray(jsonObject.getString("items"))
                    for (int j = 0; j < itemArray.length(); j++){
                        JSONObject itemjson = new JSONObject(itemarray.getString(i))
                        ItemInfo itemInfo = new ItemInfo();

                        itemInfo.setID(itemjson.getString("id"));
                        itemInfo.setName(itemjson.getString("name"));
                        itemInfo.setDescription(itemjson.getString("description"));
                        itemInfo.setPivot(itemjson.getString("pivot"));
                        //pivot is still formatted as JSON

                        purchasedInfo.itemInfoClass.add(itemInfo);


                    }


                    purchasedInfo.setI_price(jsonObject.getString("price"));
                    purchasedInfo.setI_image(jsonObject.getString("image"));
                    purchasedInfos.add(purchasedInfo);
                    }
}

答案 1 :(得分:0)

如果您使用GSON,Jackson或其他类似的库解析JSON响应,将会更容易。然后,您只需使用List模型。

http://www.jsonschema2pojo.org/

https://github.com/google/gson

http://www.ssaurel.com/blog/learn-how-to-parse-json-in-android-with-gson/