android 解析json数据格式

来源:互联网 发布:windows系统iso下载 编辑:程序博客网 时间:2024/05/18 02:19
json数据格式解析我自己分为两种;

一种是普通的,一种是带有数组形式的;


 

普通形式的:

服务器端返回的json数据格式如下:

{"userbean":{"Uid":"100196","Showname":"\u75af\u72c2\u7684\u7334\u5b50","Avtar":null,"State":1}}

分析代码如下:

// TODO 状态处理 500 200

 int res = 0;                 res = httpClient.execute(httpPost).getStatusLine().getStatusCode();                 if (res == 200) {                     /*                      * 当返回码为200时,做处理                      * 得到服务器端返回json数据,并做处理                      * */                     HttpResponse httpResponse = httpClient.execute(httpPost);                     StringBuilder builder = new StringBuilder();                     BufferedReader bufferedReader2 = new BufferedReader(                             new InputStreamReader(httpResponse.getEntity().getContent()));                     String str2 = "";                     for (String s = bufferedReader2.readLine(); s != null; s = bufferedReader2                             .readLine()) {                         builder.append(s);                     }                     Log.i("cat", ">>>>>>" + builder.toString());JSONObject jsonObject = new JSONObject(builder.toString())                         .getJSONObject("userbean");                 String Uid;                 String Showname;                 String Avtar;                 String State;                 Uid = jsonObject.getString("Uid");                 Showname = jsonObject.getString("Showname");                 Avtar = jsonObject.getString("Avtar");                 State = jsonObject.getString("State");

带数组形式的:
服务器端返回的数据格式为:


{"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}
            ]
    }
}

分析代码如下:

// TODO 状态处理 500 200                 int res = 0;                 res = httpClient.execute(httpPost).getStatusLine().getStatusCode();                 if (res == 200) {                     /*                      * 当返回码为200时,做处理                      * 得到服务器端返回json数据,并做处理                      * */                     HttpResponse httpResponse = httpClient.execute(httpPost);                     StringBuilder builder = new StringBuilder();                     BufferedReader bufferedReader2 = new BufferedReader(                             new InputStreamReader(httpResponse.getEntity().getContent()));                     String str2 = "";                     for (String s = bufferedReader2.readLine(); s != null; s = bufferedReader2                             .readLine()) {                         builder.append(s);                     }                     Log.i("cat", ">>>>>>" + builder.toString());                     /**                      * 这里需要分析服务器回传的json格式数据,                      */                     JSONObject jsonObject = new JSONObject(builder.toString())                             .getJSONObject("calendar");                     JSONArray jsonArray = jsonObject.getJSONArray("calendarlist");                     for(int i=0;i<jsonArray.length();i++){                         JSONObject jsonObject2 = (JSONObject)jsonArray.opt(i);                         CalendarInfo calendarInfo = new CalendarInfo();                         calendarInfo.setCalendar_id(jsonObject2.getString("calendar_id"));                         calendarInfo.setTitle(jsonObject2.getString("title"));                         calendarInfo.setCategory_name(jsonObject2.getString("category_name"));                         calendarInfo.setShowtime(jsonObject2.getString("showtime"));                         calendarInfo.setEndtime(jsonObject2.getString("endshowtime"));                         calendarInfo.setAllDay(jsonObject2.getBoolean("allDay"));                         calendarInfos.add(calendarInfo);                     }

返回数组,且其中标签名字不确定的

服务器端返回数据格式:

{
    "todoList": {
        "Android\u7248\u672c\u5b9e\u73b0": [
            {
                "Id": "64",
                "TodosId": "21",
                "TopicsId": "75",
                "Uid": "20",
                "TodoUid": "21",
                "DoneUid": "21",
                "Title": "\u5854\u7f57\u724c\u529f\u80fd\u5b9e\u73b0",
                "Fnishtime": "0",
                "State": "1",
                "Donetime": "1359713325",
                "Updatetime": "1358906177",
                "ListTitle": "Android\u7248\u672c\u5b9e\u73b0",
                "pid": "8",
                "ListId": "21",
                "UserName": "\u738b\u91d1\u6d9b",
                "Comment": "0",
                "ProjectsName": "\u60c5\u4eba\u8282\u4e13\u9898\u58c1\u7eb8"
            },
            {
                "Id": "63",
                "TodosId": "21",
                "TopicsId": "74",
                "Uid": "20",
                "TodoUid": "21",
                "DoneUid": "21",
                "Title": "\u754c\u9762\u5207\u56fe\u66ff\u6362",
                "Fnishtime": "0",
                "State": "1",
                "Donetime": "1359713324",
                "Updatetime": "1358906174",
                "ListTitle": "Android\u7248\u672c\u5b9e\u73b0",
                "pid": "8",
                "ListId": "21",
                "UserName": "\u738b\u91d1\u6d9b",
                "Comment": "5",
                "ProjectsName": "\u60c5\u4eba\u8282\u4e13\u9898\u58c1\u7eb8"
            }
        ]
    },
    "projectList": [
        {
            "Id": "2",
            "TeamsId": "1",
            "Title": "\u8fdc\u53e4\u56fe\u817e",
            "Description": "\u6e38\u620f\u300a\u8fdc\u53e4\u56fe\u817e\u300b\u3001\u300aAncient flag\u300b\uff0c\u53ea\u8981\u5c06\u76f8\u540c\u7684\u4e24\u5f20\u724c\u7528\u4e09\u6839\u4ee5\u5185\u7684\u76f4\u7ebf\u8fde\u5728\u4e00\u8d77\u5c31\u53ef\u4ee5\u6d88\u9664\uff0c\u89c4\u5219\u7b80\u5355\u5bb9\u6613\u4e0a\u624b\u3002",
            "OwnerId": "15",
            "Archive": "0",
            "Updatetime": "1358408107"
        },
        {
            "Id": "3",
            "TeamsId": "1",
            "Title": "Todoo_Dug",
            "Description": "\u6b22\u8fce\u5927\u5bb6\u63d0\u4ea4BUG",
            "OwnerId": "1",
            "Archive": "0",
            "Updatetime": "1358409824"
        },
        {
            "Id": "6",
            "TeamsId": "1",
            "Title": "\u719f\u6089Todoo",
            "Description": "\u5de5\u6b32\u5584\u5176\u4e8b\uff0c\u5fc5\u5148\u5229\u5176\u5668",
            "OwnerId": "15",
            "Archive": "0",
            "Updatetime": "1358411844"
        },
        {
            "Id": "8",
            "TeamsId": "1",
            "Title": "\u60c5\u4eba\u8282\u4e13\u9898\u58c1\u7eb8",
            "Description": "\u60c5\u4eba\u8282\u4e3b\u9898\u58c1\u7eb8",
            "OwnerId": "14",
            "Archive": "0",
            "Updatetime": "1358756177"
        },
        {
            "Id": "12",
            "TeamsId": "1",
            "Title": "\u58c1\u7eb8\u4e13\u9898",
            "Description": "iOS\uff0c\u5b89\u5353\u58c1\u7eb8\u8f6f\u4ef6\u96c6\u5408",
            "OwnerId": "14",
            "Archive": "0",
            "Updatetime": "1361253119"
        },
        {
            "Id": "13",
            "TeamsId": "1",
            "Title": "Todoo\u5ba2\u6237\u7aef\u9879\u76ee",
            "Description": "iphone,ipad\u7248todoo",
            "OwnerId": "14",
            "Archive": "0",
            "Updatetime": "1361600060"
        }
    ]
    
}

解析代码:

try {List<UserTodo> userTodosList = new ArrayList<UserTodo>();List<UserProject> userProjectsList = new ArrayList<UserProject>();JSONObject jsonObject = new JSONObject(jsonStr);JSONObject todoObject = (JSONObject) jsonObject.optJSONObject("todoList");JSONArray projectArray = jsonObject.getJSONArray("projectList");if (todoObject != null) {JSONArray todoArray = todoObject.names();for (int i = 0; i < todoArray.length(); i++) {JSONArray userTodos = todoObject.optJSONArray(todoArray.getString(i));for (int j = 0; j < userTodos.length(); j++) {JSONObject userTodo = userTodos.optJSONObject(j);UserTodo todo = new UserTodo();todo.setId(userTodo.optInt(UserTodo.FIELD_ID));todo.setTitle(userTodo.optString(UserTodo.FIELD_TITLE));userTodosList.add(todo);}}}TDUserTodoDao userTodoDao = new TDUserTodoDao(context);userTodoDao.deleteAll();userTodoDao.addAll(userTodosList);for (int i = 0; i < projectArray.length(); i++) {JSONObject userProject = (JSONObject) projectArray.opt(i);UserProject project = new UserProject();project.setId(userProject.optInt(UserProject.FIELD_ID));project.setTitle(userProject.optString(UserProject.FIELD_TITLE));userProjectsList.add(project);}TDUserProjectDao userProjectDao = new TDUserProjectDao(context);userProjectDao.deleteAll();userProjectDao.addAll(userProjectsList);} catch (JSONException e) {e.printStackTrace();}
该例子只解析了部分需要的属性。

总结,普通形式的只需用JSONObject ,带数组形式的需要使用JSONArray 将其变成一个list。


原创粉丝点击