Parsing JSON object in android

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

I have JSON object as follows:

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


        
5条回答
  •  情书的邮戳
    2020-12-04 03:12

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

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

提交回复
热议问题