【问题标题】:Android can't parse JSON dataAndroid 无法解析 JSON 数据
【发布时间】:2015-03-12 05:49:37
【问题描述】:

这是我的 JSON:

{
 "columns": [
    "logoFileName",
    "logoFileName1",
    "companyEventIDUnique",
    "Date",
    "Event",
    "soldOut",
    "companyEventGroupDescription",
    "eventImageFileName",
    "teamsImagePath"
],
"rows": [
    [
        "RoyalChallengersBangalore11.png",
        "SH11.png",
        14,
        "Monday, 13 Apr 2015, 8:00PM",
        "Royal Challengers Bangalore Vs Sunrisers Hyderabad ",
        0,
        "RCB Home Games 2015",
        "rcb-srh.png",
        "images/upload/team/"
    ],
    [
        "RoyalChallengersBangalore11.png",
        "MumbaiIndiansLogo.png",
        15,
        "Sunday, 19 Apr 2015, 8:00PM",
        "Royal Challengers Bangalore Vs Mumbai Indians ",
        0,
        "RCB Home Games 2015",
        "rcb-mi.png",
        "images/upload/team/"
    ],
    [
        "RoyalChallengersBangalore11.png",
        "ChennaiSuperKingsLogo11.png",
        16,
        "Wednesday, 22 Apr 2015, 8:00PM",
        "Royal Challengers Bangalore Vs Chennai Super Kings ",
        0,
        "RCB Home Games 2015",
        "rcb-csk.png",
        "images/upload/team/"
    ],
    [
        "RoyalChallengersBangalore11.png",
        "RajasthanRoyalsLogo11.png",
        17,
        "Wednesday, 29 Apr 2015, 8:00PM",
        "Royal Challengers Bangalore Vs Rajasthan Royals ",
        0,
        "RCB Home Games 2015",
        "rcb-rr.png",
        "images/upload/team/"
    ],
    [
        "RoyalChallengersBangalore11.png",
        "KKR_logo.png",
        18,
        "Saturday, 02 May 2015, 4:00PM",
        "Royal Challengers Bangalore Vs Kolkata Knight Riders ",
        0,
        "RCB Home Games 2015",
        "rcb-kkr.png",
        "images/upload/team/"
    ],
    [
        "RoyalChallengersBangalore11.png",
        "KingsXIPunjabLogo11.png",
        19,
        "Wednesday, 06 May 2015, 8:00PM",
        "Royal Challengers Bangalore Vs Kings XI Punjab ",
        0,
        "RCB Home Games 2015",
        "rcb-kxip.png",
        "images/upload/team/"
    ],
    [
        "RoyalChallengersBangalore11.png",
        "DelhiDaredevilsLogo12.png",
        20,
        "Sunday, 17 May 2015, 4:00PM",
        "Royal Challengers Bangalore Vs Delhi DareDevils ",
        0,
        "RCB Home Games 2015",
        "rcb-dd.png",
        "images/upload/team/"
    ]
]
 }

我无法解析它。请帮助我如何解析它。

【问题讨论】:

  • 尝试使用 Gson 库.....
  • 你能解释一下我从未使用过 GSON
  • 你在解析时遇到了什么问题?
  • 您可以简单地使用json解析器,请解释您的问题或显示一些代码
  • 你能发布你目前拥有的代码吗?您是否尝试过使用 JSONObject 和 JSONArray 来解析这些数据?

标签: android json


【解决方案1】:

你可以像下面的代码一样解析它-

try{
        String result = "YOUR JSON DATA";
        JSONObject json = new JSONObject(result);
        JSONArray jArrayColumns = json.getJSONArray("columns"); 
        for(int i=0;i<jArrayColumns.length();i++){
            String data = jArrayColumns.getString(i);
            Log.i("TAG", "JSON ARRAY"+data);
        }
        JSONArray jArrayRows = json.getJSONArray("rows"); 
        for(int i=0;i<jArrayRows.length();i++){
            JSONArray jRowItem = jArrayColumns.getJSONArray(i);
            for(int j=0;j<jRowItem.length();j++){
                String rowItem = jRowItem.getString(j);
            }
            Log.i("TAG", "JSON ARRAY rowItem"+rowItem);
        }


    }
    catch(Exception e){
        e.printStackTrace();
    }

【讨论】:

  • 如何合理安排,方便使用图片和其他活动?
  • json 响应中没有 key 或 flag,所以你必须自己管理它。或者您可以通过在键值对中传递数据来更新来自您的 Web 服务的 json 响应。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-15
  • 1970-01-01
  • 2016-11-19
  • 1970-01-01
  • 2019-02-24
  • 2020-12-05
相关资源
最近更新 更多