在Android中解析JSON对象 [英] Parsing JSON object in android

查看:104
本文介绍了在Android中解析JSON对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有如下的JSON对象:

I have JSON object as follows:

[
    {
        "Project": {
            "id": "1",
            "project_name": "name"
        },
        "AllocationDetail": [
            {
                "id": "1",
                "project_id": "1",
                "team_name": "ror",
                "week_percentage_work": "50",
                "in_today": "1",
                "actual_hours": "30",
                "remaining_hours": "100",
                "time_difference": null,
                "created": "2012-01-13 15:48:33",
                "modified": "2012-01-13 15:48:33"
            },
            {
                "id": "2",
                "project_id": "1",
                "team_name": "php",
                "week_percentage_work": "40",
                "in_today": "2",
                "actual_hours": "50",
                "remaining_hours": "100",
                "time_difference": null,
                "created": "2012-01-13 15:49:40",
                "modified": "2012-01-13 15:49:40"
            }
        ]
    }
]

我想在android中解析数据并将其存储到DB中,但是我对Jsonobject感到困惑 谢谢

I want to parse data in android and store it into DB, but i m getting confused in Jsonobject thanks

推荐答案

以下是用于解析json字符串的代码段.请经历一下:

The following is a snippet code for parsing your json string. Kindly go through it:

    String response = <Your JSON String>;
            String Project = null;
            String AllocationDetail = null;
            try {
                JSONArray menuObject = new JSONArray(response);
                 for (int i = 0; i< menuObject.length(); i++) {
                        Project     =   menuObject.getJSONObject(i).getString("Project").toString();
                        System.out.println("Project="+Project);
                        AllocationDetail    =   menuObject.getJSONObject(i).getString("AllocationDetail").toString();
                        System.out.println("AllocationDetail="+AllocationDetail);
                 }
                 JSONObject jsonObject  =   new JSONObject(Project);
                 String id = jsonObject.getString("id");
                 System.out.println("id="+id);
                 String project_name = jsonObject.getString("project_name");
                 System.out.println("project_name="+project_name);

                 JSONArray jArray = new JSONArray(AllocationDetail);
                 for (int i = 0; i< jArray.length(); i++) {
                     String team_name       =   jArray.getJSONObject(i).getString("team_name").toString();
                     System.out.println("team_name="+team_name);
                     String week_percentage_work        =   jArray.getJSONObject(i).getString("week_percentage_work").toString();
                     System.out.println("week_percentage_work="+week_percentage_work);
                 }
            } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

这篇关于在Android中解析JSON对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆