JAVA parser for asana json data "Error:JSONObject cannot be converted to JSONArray" -
i working on java parser parse asana json data. executing request using curl , curl use cygwin tool(used execute unix commands on windows).
now getting single project details in json format(that contain tasks in projects) getting 1 task id , use task id single task details. when parse subrequest data(request single task detail). gives me error "jsonobject cannot converted jsonarray".
error line
jsonarray dataarray2= (jsonarray)jsonobject2.get("data");
example asana json response many tasks
{ "data": [ { "id": 23097910271884, "name": "asana prtl rprtng intgrtn r&d" }, { "id": 30483134480109, "name": "curl training" }, { "id": 30483134480115, "name": "database design" }, { "id": 31806291283828, "name": "json parser setup" }, { "id": 31806291283832, "name": "jdb connectivity , setup" }, { "id": 30483134480118, "name": "java utility development" }, { "id": 31806291283837, "name": "data synchronization testing" }, { "id": 31806291283841, "name": "bug fixes" }, { "id": 30483134480171, "name": "portal test report development (1)" } ] }
example asana single task detail
{ "data": { "id": 23097910271884, "created_at": "2014-12-30t06:35:11.959z", "modified_at": "2015-05-12t07:19:02.589z", "name": "asana prtl rprtng intgrtn r&d", "notes": "", "completed": true, "assignee_status": "inbox", "completed_at": "2015-05-12t07:20:29.181z", "due_on": "2015-04-01", "due_at": null, "workspace": { "id": 14810798216415, "name": "crescentbahuman.com" }, "num_hearts":2, "parent": null, "tags": [ { "id": 17129100409445, "name": "it" } ], "projects": [ { "id": 30483134480107, "name": "asana integrations" } ], "hearted": true, "memberships": [ { "project": { "id": 30483134480107, "name": "asana integrations" }, "section": null } ], "assignee": { "id": 22273357407644, "name": "abdul majid bajwa" }, "hearts": [ { "id": 33739876272211, "user": { "id": 22273357407644, "name": "abdul majid bajwa" } }, { "id": 33740502736864, "user": { "id": 14810783571926, "name": "asana admin" } } ], "followers": [ { "id": 14810783571926, "name": "asana admin" }, { "id": 22273357407644, "name": "abdul majid bajwa" } ] } }
example java code
package alljsonclasses; import java.io.*; import java.lang.*; import org.json.simple.jsonarray; import org.json.simple.jsonobject; import org.json.simple.parser.jsonparser; public class asanadata { public static void main (string args[]){ file workdir = new file("c:/cygwin64/bin"); try { runtime systemshell = runtime.getruntime(); string urlrequest="curl -u 6k8mjnvi.o6s1dqbgt1aiesl4kqsqb0g: https://app.asana.com/api/1.0/projects/30483134480107/tasks?opt_pretty"; string cmd = urlrequest; cmd += " | grep 'object'"; process shelloutput = systemshell.exec(workdir+"/"+cmd, null, workdir); inputstreamreader isr = new inputstreamreader(shelloutput.getinputstream()); bufferedreader br = new bufferedreader (isr); jsonparser jsonparser = new jsonparser(); jsonobject jsonobject = (jsonobject) jsonparser.parse(br); jsonarray dataarray= (jsonarray) jsonobject.get("data"); for(int i=0;i<dataarray.size();i++){ jsonobject dataobject = (jsonobject) dataarray.get(i); long task_id = (long) dataobject.get("id"); system.out.println("project id is: " + task_id); try { file workdir2 = new file("c:/cygwin64/bin"); runtime systemshell2 = runtime.getruntime(); string curlrequest2="curl -u 6k8mjnvi.o6s1dqbgt1aiesl4kqsqb0g: https://app.asana.com/api/1.0/tasks/"+task_id+"?opt_pretty"; system.out.println(); string cmd2 = curlrequest2; cmd2 += " | grep 'object'"; process shelloutput2 = systemshell2.exec(workdir2+"/"+cmd2, null, workdir2); inputstreamreader isr2 = new inputstreamreader(shelloutput2.getinputstream()); bufferedreader br2 = new bufferedreader (isr2); jsonparser jsonparser2 = new jsonparser(); jsonobject jsonobject2 = (jsonobject) jsonparser2.parse(br2); jsonarray dataarray2= (jsonarray)jsonobject2.get("data"); for(int i2=0;i<dataarray2.size();i2++){ jsonobject dataobject2 = (jsonobject) dataarray2.get(i2); long task_id2 = (long) dataobject2.get("id"); system.out.println("task id is: " + task_id2); string task_created_at_dt2 = (string) dataobject2.get("created_at"); system.out.println("the task created @ is: " + task_created_at_dt2); string task_modified_at_dt2 = (string) dataobject2.get("modified_at"); system.out.println("the task modified @ is: " + task_modified_at_dt2); string task_name = (string) dataobject2.get("name"); system.out.println("task name is: " + task_name); string task_notes = (string) dataobject2.get("notes"); system.out.println("task notes are: " + task_notes); boolean task_completed_status = (boolean) dataobject2.get("completed"); system.out.println("task completed status: " + task_completed_status); string task_assignee_status = (string) dataobject2.get("assignee_status"); system.out.println("task assignee status: " + task_assignee_status); string completed_at = (string) dataobject2.get("completed_at"); system.out.println("task completed at: " + completed_at); string task_due_on = (string) dataobject2.get("due_on"); system.out.println("task due date: " + task_due_on); string due_at = (string) dataobject2.get("due_at"); system.out.println("task due at: " + due_at); jsonobject workspaceobj = (jsonobject) dataobject2.get("workspace"); long workspace_id = (long) workspaceobj.get("id"); string workspace_name = (string) workspaceobj.get("name"); system.out.println("workspace id " + workspace_id + " workspace name " + workspace_name); long task_number_of_hearts = (long) dataobject2.get("num_hearts"); system.out.println("task number of hearts: " + task_number_of_hearts); string task_parent = (string) dataobject2.get("parent"); system.out.println("task parent: " + task_parent); jsonarray task_hearts= (jsonarray) dataobject2.get("hearts"); (int z = 0; z < task_hearts.size(); z++) { jsonobject task_hearts_object = (jsonobject) task_hearts.get(z); long task_hearts_id = (long) task_hearts_object.get("id"); string task_hearts_name = (string) task_hearts_object.get("name"); system.out.println("task hearts id " + task_hearts_id + " task hearts name " + task_hearts_name); } jsonarray task_tags= (jsonarray) dataobject2.get("tags"); (int z = 0; z < task_tags.size(); z++) { jsonobject task_tags_object = (jsonobject) task_tags.get(z); long task_tags_id = (long) task_tags_object.get("id"); string task_tags_name = (string) task_tags_object.get("name"); system.out.println("task tags id " + task_tags_id + " task tags name " + task_tags_name); } boolean task_hearted = (boolean) dataobject2.get("hearted"); system.out.println("task hearted: " + task_hearted); jsonarray task_projects= (jsonarray) dataobject2.get("projects"); (int z = 0; z < task_projects.size(); z++) { jsonobject task_projects_object = (jsonobject) task_projects.get(z); long task_projects_id = (long) task_projects_object.get("id"); string task_projects_name = (string) task_projects_object.get("name"); system.out.println("task projects id " + task_projects_id + " task projects name " + task_projects_name); } jsonarray task_memberships= (jsonarray) dataobject2.get("memberships"); (int z = 0; z < task_memberships.size(); z++) { jsonobject task_memberships_object = (jsonobject) task_memberships.get(z); jsonobject task_memberships_section_object = (jsonobject) task_memberships_object.get("section"); if(task_memberships_section_object!=null){ long task_memberships_section_id = (long) task_memberships_section_object.get("id"); string task_memberships_section_name = (string) task_memberships_section_object.get("name"); system.out.println("task memberships section id " + task_memberships_section_id + " task memberships section name " + task_memberships_section_name); } jsonobject task_memberships_project = (jsonobject) task_memberships_object.get("project"); long task_memberships_project_id = (long) task_memberships_project.get("id"); string task_memberships_project_name = (string) task_memberships_project.get("name"); system.out.println("task memberships project id " + task_memberships_project_id + " task memberships project name " + task_memberships_project_name); } jsonobject task_assignee_object = (jsonobject) dataobject2.get("assignee"); if(task_assignee_object!=null){ long task_assignee_id = (long) task_assignee_object.get("id"); string task_assignee_name = (string) task_assignee_object.get("name"); system.out.println("assignee id " + task_assignee_id + " assignee name " + task_assignee_name); } jsonarray task_followers= (jsonarray) dataobject2.get("followers"); (int z = 0; z < task_followers.size(); z++) { jsonobject task_followers_object = (jsonobject) task_followers.get(z); long task_followers_id = (long) task_followers_object.get("id"); string task_followers_name = (string) task_followers_object.get("name"); system.out.println("followers id " + task_followers_id + " followers name " + task_followers_name); } jsonarray task_subtasks= (jsonarray) dataobject2.get("subtasks"); for(int j=0;j<task_subtasks.size();j++){ jsonobject sub_data_object = (jsonobject) task_subtasks.get(j); //jsonobject innerobj = (jsonobject) m.get("project"); long sub_task_id = (long) sub_data_object.get("id"); system.out.println("subtasks id is: " + sub_task_id); string sub_task_name = (string) sub_data_object.get("name"); system.out.println("subtasks task name is: " + sub_task_name); string sub_task_notes = (string) sub_data_object.get("notes"); system.out.println("subtasks notes are: " + sub_task_notes); boolean sub_task_completed_status = (boolean) sub_data_object.get("completed"); system.out.println("subtasks completed status: " + sub_task_completed_status); string sub_task_assignee_status = (string) sub_data_object.get("assignee_status"); system.out.println("subtasks assignee status: " + sub_task_assignee_status); string sub_completed_at = (string) sub_data_object.get("completed_at"); system.out.println("subtasks completed at: " + sub_completed_at); string sub_task_due_on = (string) sub_data_object.get("due_on"); system.out.println("subtasks due on: " + sub_task_due_on); string sub_due_at = (string) sub_data_object.get("due_at"); system.out.println("subtasks due at: " + sub_due_at); jsonobject sub_workspace_object = (jsonobject) sub_data_object.get("workspace"); long sub_workspace_id = (long) sub_workspace_object.get("id"); string sub_workspace_name = (string) sub_workspace_object.get("name"); system.out.println("subtasks workspace id " + sub_workspace_id + " subtasks workspace name " + sub_workspace_name); long sub_task_number_of_hearts = (long) sub_data_object.get("num_hearts"); system.out.println("subtasks number of hearts: " + sub_task_number_of_hearts); jsonobject sub_parent_object = (jsonobject) sub_data_object.get("parent"); long sub_parent_id = (long) sub_parent_object.get("id"); string sub_parent_name = (string) sub_parent_object.get("name"); system.out.println("subtasks parent id " + sub_parent_id + " subtasks parent name " + sub_parent_name); jsonarray sub_hearts= (jsonarray) sub_data_object.get("hearts"); (int z = 0; z < sub_hearts.size(); z++) { jsonobject sub_hearts_object = (jsonobject) sub_hearts.get(z); long sub_hearts_id = (long) sub_hearts_object.get("id"); string sub_hearts_name = (string) sub_hearts_object.get("name"); system.out.println("subtasks hearts id " + sub_hearts_id + "with subtasks hearts name " + sub_hearts_name); } jsonarray sub_tags= (jsonarray) sub_data_object.get("tags"); (int z = 0; z < sub_tags.size(); z++) { jsonobject sub_tags_object = (jsonobject) sub_tags.get(z); long sub_tags_id = (long) sub_tags_object.get("id"); string sub_tags_name = (string) sub_tags_object.get("name"); system.out.println("subtasks tags id " + sub_tags_id + " subtasks tags name " + sub_tags_name); } boolean sub_task_hearted = (boolean) sub_data_object.get("hearted"); system.out.println("subtasks hearted: " + sub_task_hearted); jsonarray sub_projects= (jsonarray) sub_data_object.get("projects"); (int z = 0; z < sub_projects.size(); z++) { jsonobject sub_projects_object = (jsonobject) sub_projects.get(z); long sub_projects_id = (long) sub_projects_object.get("id"); string sub_projects_name = (string) sub_projects_object.get("name"); system.out.println("subtasks projects id " + sub_projects_id + " subtasks projects name " + sub_projects_name); } jsonarray sub_memberships= (jsonarray) sub_data_object.get("memberships"); if(sub_memberships!=null){ (int z = 0; z < sub_memberships.size(); z++) { jsonobject sub_memberships_object = (jsonobject) sub_memberships.get(z); jsonobject sub_memberships_section_object = (jsonobject) sub_memberships_object.get("section"); long sub_memberships_section_id = (long) sub_memberships_section_object.get("id"); string sub_memberships_section_name = (string) sub_memberships_section_object.get("name"); system.out.println("subtasks memberships section id " + sub_memberships_section_id + " subtasks memberships section name " + sub_memberships_section_name); jsonobject sub_memberships_project_object = (jsonobject) sub_memberships_object.get("project"); long sub_memberships_project_id = (long) sub_memberships_project_object.get("id"); string sub_memberships_project_name = (string) sub_memberships_project_object.get("name"); system.out.println("subtasks memberships project id " + sub_memberships_project_id + " subtasks memberships project name " + sub_memberships_project_name); } } jsonobject sub_assignee_object = (jsonobject) sub_data_object.get("assignee"); if(sub_assignee_object!=null){ long sub_assignee_id = (long) sub_assignee_object.get("id"); string sub_assignee_name = (string) sub_assignee_object.get("name"); system.out.println("subtasks assignee id " + sub_assignee_id + " subtasks assignee name " + sub_assignee_name); } jsonarray sub_followers= (jsonarray) sub_data_object.get("followers"); (int z = 0; z < sub_followers.size(); z++) { jsonobject sub_followers_object = (jsonobject) sub_followers.get(z); long sub_followers_id = (long) sub_followers_object.get("id"); string sub_followers_name = (string) sub_followers_object.get("name"); system.out.println("subtasks followers id " + sub_followers_id + " subtasks followers name " + sub_followers_name); }}} }catch (ioexception ioe){ system.err.println(ioe); }catch (throwable t) { t.printstacktrace(); } string task_name = (string) dataobject.get("name"); system.out.println("project name is: " + task_name); } int exitval = shelloutput.waitfor(); system.out.println("process exit value : "+exitval); }catch (ioexception ioe){ system.err.println(ioe); }catch (throwable t) { t.printstacktrace(); } } }
according docs have use getjsonarray()
method instead of simple get()
:
jsonarray dataarray2 = jsonobject2.getjsonarray("data");
i hope fixes error.
Comments
Post a Comment