Parsing JSON object in android

前端 未结 5 527
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-04 02:31

I have JSON object as follows:

[
    {
        \"Project\": {
            \"id\": \"1\",
            \"project_name\": \"name\"
        },
        \"Allocati         


        
相关标签:
5条回答
  • 2020-12-04 03:02

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

    0 讨论(0)
  • 2020-12-04 03:12

    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();
            }
    
    0 讨论(0)
  • 2020-12-04 03:21

    There are two ways for parsing JSON

    • Google GSON
    • 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.

    0 讨论(0)
  • 2020-12-04 03:21

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

    0 讨论(0)
  • 2020-12-04 03:23
    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.

    0 讨论(0)
提交回复
热议问题