这是一个程序,使用来自url的curl获取asana json数据,然后解析它。但是当我运行此程序时,错误显示
java.lang.ClassCastException:org.json.simple.JSONObject无法强制转换为org.json.simple.JSONArray 在jsontest.GetJsonDataAndParseIt.main(GetJsonDataAndParseIt.java:44)
代码文件:
<% @habit.levels.each_with_index do |level, index| %>
<%= check_box_tag "missed_days[#{level}][1]", true, level.missed_days > 0, {class: "habit-check"} %>
<%= check_box_tag "missed_days[#{level}][2]", true, level.missed_days > 0, {class: "habit-check"} %>
<%= check_box_tag "missed_days[#{level}][3]", true, level.missed_days > 0, {class: "habit-check"} %>
<% end %>
Json文件
package jsontest;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
public class GetJsonDataAndParseIt {
public static void main (String args[]){
File workDir = new File("C:/cygwin64/bin");
try {
Runtime systemShell = Runtime.getRuntime();
String cmd = "curl -u 4cKDgv4O.L8Th7N8l7jADg3HRU44abmT:https://app.asana.com/api/1.0/tasks/26673876577868?opt_pretty";
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("Task id is: " + task_id);
String task_name = (String) dataObject.get("name");
System.out.println("Task name is: " + task_name);
String task_notes = (String) dataObject.get("notes");
System.out.println("Task Notes are: " + task_notes);
boolean task_completed_status = (boolean) dataObject.get("completed");
System.out.println("Task Completed status: " + task_completed_status);
String task_assignee_status = (String) dataObject.get("assignee_status");
System.out.println("Task Assignee status: " + task_assignee_status);
String completed_at = (String) dataObject.get("completed_at");
System.out.println("Task Completed at: " + completed_at);
String task_due_on = (String) dataObject.get("due_on");
System.out.println("Task due date: " + task_due_on);
String due_at = (String) dataObject.get("due_at");
System.out.println("Task due at: " + due_at);
JSONObject workspaceObj = (JSONObject) dataObject.get("workspace");
Long workspace_id = (Long) workspaceObj.get("id");
String workspace_name = (String) workspaceObj.get("name");
System.out.println("Workspace id " + workspace_id + " with workspace name " + workspace_name);
long task_number_of_hearts = (long) dataObject.get("num_hearts");
System.out.println("Task number of hearts: " + task_number_of_hearts);
String task_parent = (String) dataObject.get("parent");
System.out.println("Task parent: " + task_parent);
JSONArray task_hearts= (JSONArray) dataObject.get("hearts");
for (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 + " with Task hearts name " + task_hearts_name);
}
JSONArray task_tags= (JSONArray) dataObject.get("tags");
for (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 + " with Task tags name " + task_tags_name);
}
boolean task_hearted = (boolean) dataObject.get("hearted");
System.out.println("Task hearted: " + task_hearted);
JSONArray task_projects= (JSONArray) dataObject.get("projects");
for (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 + " with Task projects name " + task_projects_name);
}
JSONArray task_memberships= (JSONArray) dataObject.get("memberships");
for (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 + " with 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 + " with Task memberships project name " + task_memberships_project_name);
}
JSONObject task_assignee_object = (JSONObject) dataObject.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 + " with assignee name " + task_assignee_name);
}
JSONArray task_followers= (JSONArray) dataObject.get("followers");
for (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 + " with followers name " + task_followers_name);
}
JSONArray task_subtasks= (JSONArray) dataObject.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 + " with 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 + " with Subtasks parent name " + sub_parent_name);
JSONArray sub_hearts= (JSONArray) sub_data_object.get("hearts");
for (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");
for (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 + " with 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");
for (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 + " with Subtasks projects name " + sub_projects_name);
}
JSONArray sub_memberships= (JSONArray) sub_data_object.get("memberships");
if(sub_memberships!=null){
for (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 + " with 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 + " with 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 + " with Subtasks assignee name " + sub_assignee_name);
}
JSONArray sub_followers= (JSONArray) sub_data_object.get("followers");
for (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 + " with Subtasks followers name " + sub_followers_name);
}}}
int exitVal = shellOutput.waitFor();
System.out.println("Process Exit Value : "+exitVal);
}catch (IOException ioe){
System.err.println(ioe);
}catch (Throwable t) {
t.printStackTrace();
}
}
}
答案 0 :(得分:2)
在您提供的| Error 2015-04-30 10:52:09,868 [localhost-startStop-1] ERROR
context.GrailsContextLoaderListener - Error initializing the
application: Error creating bean with name 'grailsApplication'
defined in ServletContext resource [/WEB-INF/applicationContext.xml]:
Invocation of init method failed; nested exception is
org.codehaus.groovy.grails.exceptions.GrailsConfigurationException:
Class not found loading Grails application: upgrade-2.1
Message: Error creating bean with name 'grailsApplication' defined
in ServletContext resource [/WEB-INF/applicationContext.xml]:
Invocation of init method failed; nested exception is
org.codehaus.groovy.grails.exceptions.GrailsConfigurationException:
Class not found loading Grails application: upgrade-2.1
Line | Method
->> 262 | run in java.util.concurrent.FutureTask
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
| 1145 | runWorker in java.util.concurrent.ThreadPoolExecutor
| 615 | run . . . in java.util.concurrent.ThreadPoolExecutor$Worker
^ 745 | run in java.lang.Thread
Caused by GrailsConfigurationException: Class not found loading Grails application: upgrade-2.1
->> 262 | run in java.util.concurrent.FutureTask
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
| 1145 | runWorker in java.util.concurrent.ThreadPoolExecutor
| 615 | run . . . in java.util.concurrent.ThreadPoolExecutor$Worker
^ 745 | run in java.lang.Thread
Caused by ClassNotFoundException: upgrade-2.1
->> 366 | run in java.net.URLClassLoader$1
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
| 355 | run in ''
| 354 | findClass in java.net.URLClassLoader
| 425 | loadClass in java.lang.ClassLoader
| 262 | run . . . in java.util.concurrent.FutureTask
| 1145 | runWorker in java.util.concurrent.ThreadPoolExecutor
| 615 | run . . . in java.util.concurrent.ThreadPoolExecutor$Worker
^ 745 | run in java.lang.Thread
的JSON文件中,data
是JSONObject
,而不是JSONArray
,因此此行可能会引发异常:
JSONArray dataArray= (JSONArray) jsonObject.get("data");
只需将dateArray
更改为JSONObject(也可能将其名称更改为dataObject
或类似名称),并将其作为键值对象处理,而不是数组。