【问题标题】:JSON Array Splitter in AndroidAndroid 中的 JSON 数组拆分器
【发布时间】:2014-02-07 07:06:53
【问题描述】:

我在一个涉及将 JSON 解析为列表视图的 Android 项目中工作。

JSON 文件包含一个 Array 名称和 10 组 JSON 对象字段,

我需要将前 5 组拆分到一个活动页面中,将其余 5 组拆分到下一个活动页面,当我单击按钮或任何要导航的内容时应该激活该页面。

Intent 有助于导航,但拆分 json 对我来说很复杂。

JSON 代码

{
    "Productcategory": [
        {
            "shop_cat_id": "1",
            "shop_cat_name": "kurtis",
            "shop_scat_id1": "1",
            "shop_scat_id1name": "cotton kurtis",
            "shop_scat_id2": "2",
            "shop_scat_id2name": "Cotton Designer Kurtis",
            "shop_scat_id3": "3",
            "shop_scat_id3name": "soft Designer kurtis"
        },
        {
            "shop_cat_id": "2",
            "shop_cat_name": "Sarees",
            "shop_scat_id1": "1",
            "shop_scat_id1name": "Saree 1",
            "shop_scat_id2": "2",
            "shop_scat_id2name": "Saree 2",
            "shop_scat_id3": "3",
            "shop_scat_id3name": "Saree 4",
            "shop_scat_id4": "4"
        },
        {
            "shop_cat_id": "3",
            "shop_cat_name": "Anarkkali suits",
            "shop_scat_id1": "1",
            "shop_scat_id1name": "Readymade",
            "shop_scat_id2": "2",
            "shop_scat_id2name": "Stitched",
            "shop_scat_id3": "3",
            "shop_scat_id3name": "Unstitched"
        },
        {
            "shop_cat_id": "4",
            "shop_cat_name": "CottonLeggins",
            "shop_scat_id1": "1",
            "shop_scat_id1name": "LSize",
            "shop_scat_id2": "2",
            "shop_scat_id2name": "XLsize",
            "shop_scat_id3": "3",
            "shop_scat_id3name": "3XLsize",
            "shop_scat_id4": "4"
        },
        {
            "shop_cat_id": "5",
            "shop_cat_name": "PattialaPantsset"
        },
        {
            "shop_cat_id": "6",
            "shop_cat_name": "Kids'AnarkkaliSuits"
        },
        {
            "shop_cat_id": "7",
            "shop_cat_name": "Kids'AnarkkaliSuits"
        },
        {
            "shop_cat_id": "8",
            "shop_cat_name": "Kids'AnarkkaliSuits"
        },
         {
            "shop_cat_id": "9",
            "shop_cat_name": "Kids'AnarkkaliSuits"
        },
        {
            "shop_cat_id": "10",
            "shop_cat_name": "Kids'AnarkkaliSuits"
        }
    ]
}

【问题讨论】:

标签: android json split


【解决方案1】:

是的,您可以使用

JsonArray productarray = jsonobj.getjsonarray("Productcategory");

现在开始计数

productarray.getCount();

并根据页码点击 获取数据数组 如果点击 1 然后从 0 到 5 和 1 然后从 5 到 10 如果你想要页数,那么

number_of_page = productarray.getCount()/5;

【讨论】:

  • 问题是当我这样做时,你说它在两页中显示前五套,后五套不显示。 @BSKANIA
  • 检查你的 for 循环开始计数。否则发布您的代码,以便任何人都可以帮助您。 Json 不是必需的。可能是您在获取数组时犯了一些错误。
  • 编码没有错误,因为我对JSON项目的拆分有疑问,如果我有10它将被5+5拆分,如果我有9那么它将如何拆分?那么第一组和第二组是如何存储的呢?
  • 然后拆分为 5 + 4。
【解决方案2】:

这是获取json数组详细信息到数组的方式。

JSONArray jsonArrContent = json.getJSONArray("Productcategory");
String [] arr_shop_cat_id;  
String [] arr_shop_cat_name;        
for (int i = 0; i < jsonArrContent.length(); i++) {
    JSONObject jsonobj = jsonArrContent.getJSONObject(i);
    // get data from the json array objetcs
    arr_shop_cat_id[i] = jsonobj.getString("shop_cat_id");
    arr_shop_cat_name[i] = jsonobj.getString("shop_cat_name");
    ...
}

在此之后,您可以根据需要轻松访问数组项。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-23
    • 2018-12-10
    • 1970-01-01
    • 1970-01-01
    • 2019-12-14
    • 2018-05-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多