程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> java-Android中關於讀取JSON數組的問題

java-Android中關於讀取JSON數組的問題

編輯:編程綜合問答
Android中關於讀取JSON數組的問題

我在讀取一個JSON數組:

JSONArray jArray = new JSONArray(jsonString);

        System.out.println("*****JARRAY*****"+jArray.length());
        for(int i=0;i<jArray.length();i++){


                JSONObject json_data = jArray.getJSONObject(i);
                Log.i("log_tag","_id"+json_data.getInt("account")+
                        ", mall_name"+json_data.getString("name")+
                        ", location"+json_data.getString("number")+
                        ", telephone"+json_data.getString("url")+
                        ",----"+json_data.getString("balance")+
                        ",----"+json_data.getString("credit")+
                        ",----"+json_data.getString("displayName")
                );

        }

JSON語法文件的案例如下:

<code>

{
    "list": [
        {
            "account": 1,
            "name": "card",
            "number": "xxxxx xxxx xxxx 2002",
            "url": "http://www.google.com",
            "balance": 1.0,
            "credit": 1.0,
            "displayName": "hsbc bank" 
        },
        {
            "account": 2,
            "name": "card2",
            "number": "xxxxx xxxx xxxx 3003",
            "url": "http://www.google.com",
            "balance": 2.0,
            "credit": 2.0,
            "displayName": "nsb bank" 
        } 
    ],
    "count": 2
}

</code>

語法前面都有一個花括號。當我執行這些代碼塊時,系統會報錯說:

A JSONArray text must start with '[' at character 1 of....

有誰遇見過這樣的問題嗎?能提供案例的代碼塊更好,不勝感激~~

最佳回答:


<string name="json_array">
    {"calendar": 
        {"calendarlist": 
            [ 
            {"calendar_id":"1705","title":"(\u4eb2\u5b50)ddssd","category_name":"\u9ed8\u8ba4\u5206\u7c7b","showtime":"1288927800","endshowtime":"1288931400","allDay":false}, 
            {"calendar_id":"1706","title":"(\u65c5\u884c)","category_name":"\u9ed8\u8ba4\u5206\u7c7b","showtime":"1288933200","endshowtime":"1288936800","allDay":false} 
            ] 
        } 
    }  </string>

解析代碼:

public void parseJsonArray(String string){
    try {
        JSONObject jsonObject = new JSONObject(string).getJSONObject("calendar");
        JSONArray jsonArray = jsonObject.getJSONArray("calendarlist");
        StringBuffer sb = new StringBuffer();
        for(int i = 0;i < jsonArray.length();i ++){
            JSONObject json = (JSONObject) jsonArray.opt(i);
            String calendar_id = json.getString("calendar_id");
            String title = json.getString("title");
            String category_name = json.getString("category_name");
            String showtime = json.getString("showtime");
            String endshowtime = json.getString("endshowtime");
            String allDay = json.getString("allDay");
            sb.append("calendar_id: " + calendar_id + "\n\n" + "title: " + title + "\n\n" + 
                    "category_name: " + category_name + "\n\n" + "showtime: " + showtime + "\n\n" + 
                    "endshowtime: " + endshowtime + "\n\n" + "allDay: " + allDay + "\n\n" + 
                    "-------------------------------------\n\n");
        }
        text.setText(sb.toString());
    } catch (JSONException e) {
        e.printStackTrace();
    }
}
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved