3

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

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

1
  • I have to srore data in a single table Commented Jan 18, 2012 at 7:18

5 Answers 5

3

the best JSON Tutorial i have seen, I leared json parsing from this tutorial, hope it will help

Sign up to request clarification or add additional context in comments.

Comments

1

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();
        }

1 Comment

Too complicate for the Newbies to understand, Please check my code for the simplest one. stackoverflow.com/questions/8906490/…
1

There are two ways for parsing JSON

When your content of JSON is too complex and long enough you can prefer usin GSON its much easier to maintain than JSON parsing each value manually.

Comments

1

Use jsonlint.com to read your json better. It appears that the json that you have copied here is invalid.

1 Comment

sorry ... this is valid one.. : [{"Project":{"id":"1","project_name":"pols"},"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"}]}]
1
String response = <Your JSON String>; // add your Json String here
response = response.replace("[","").replace("]",""); // Just remove all square braces
JSONObject json = new JSONObject(response); //Convert the Json into Json Object
JSONObject jProject = json.getJSONObject("Project"); // get your project object
String  project_id = jProject.getString("id"); // Get the value of Id
String  project_name = jProject.getString("project_name"); // get the value of Project_name

The above code can be use many times as you want to parse the Json. I know its too late, but It may help many people who are trying to find the same answer. I was trying to find the same solution, but it was too hard to use the Accepted answer because of too many lines of codes, But I got the same results with hardly few lines of codes.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.