Retrofit2 - 预期 BEGIN_ARRAY 但 BEGIN_OBJECT 使用 Call<List<JSONObject>>

Retrofit2 - Expected BEGIN_ARRAY but was BEGIN_OBJECT using Call<List<JSONObject>>

我在 Android 应用程序中使用 Retrofit2 来获取 JSON 对象,但是我不断收到一条错误消息,指出 Expected BEGIN_ARRAY but was BEGIN_OBJECT 我确实这样做了不明白为什么。

JSON:

{  
   "_id":{  
      "$oid":"59dfdcd94e835e0ed07101b3"
   },
   "campaign_id":"62400",
   "version":1,
   "sections":[  
      {  
         "next_section":"next",
         "patch_number":"",
         "patch_ext":"",
         "elements":[  
            {  
               "element":"greeting",
               "components":{  
                  "paragraph":{  
                     "content":"Hello. Thank you for calling, how may I help you?"
                  }
               },
               "guid":"0cd1d183-0568-4506-b368-5422d9c42875"
            },
            {  
               "element":"notes",
               "components":{  
                  "paragraph":{  
                     "content":"You're taking general messages from the caller."
                  }
               },
               "guid":"9924dd7b-db50-433e-a5a0-c4faae134cea"
            },
            {  
               "element":"script",
               "components":{  
                  "paragraph":{  
                     "content":"Please allow me to take a message and we will return your call as soon as possible. May I have your first and last name please?"
                  }
               },
               "guid":"fca34acd-e128-4737-836a-43c2e4725924"
            },
            {  
               "element":"custom-fullname",
               "components":{  
                  "label":{  
                     "content":""
                  },
                  "first_name":{  
                     "name":"3[custom-fullname][first_name]",
                     "label":"First",
                     "type":"text",
                     "value":"Amani",
                     "required":"",
                     "integration_mapping":""
                  },
                  "last_name":{  
                     "name":"3[custom-fullname][last_name]",
                     "label":"Last",
                     "type":"text",
                     "value":"Swann",
                     "required":"",
                     "integration_mapping":""
                  },
                  "help_text":{  
                     "content":""
                  },
                  "wf_mapping":"",
                  "visibility":{  
                     "text":true,
                     "email":true,
                     "pager":true,
                     "fax":true
                  }
               },
               "guid":"0767aae7-032f-4204-9d4e-ec19e3151719"
            },
            {  
               "element":"custom-phone",
               "components":{  
                  "label":{  
                     "content":"Phone Number"
                  },
                  "phone":{  
                     "name":"4[custom-phone][phone]",
                     "type":"text",
                     "value":"(954) 535-3634",
                     "required":"false",
                     "integration_mapping":""
                  },
                  "phone_ext":{  
                     "name":"4[custom-phone][phone_ext]",
                     "type":"text",
                     "value":""
                  },
                  "input_format":{  
                     "name":"4[custom-phone][phone_format]",
                     "type":"hidden",
                     "value":"standard"
                  },
                  "help_text":{  
                     "content":"Make sure you get caller's phone so we can call them back."
                  },
                  "wf_mapping":"",
                  "visibility":{  
                     "text":true,
                     "email":true,
                     "pager":true,
                     "fax":true
                  }
               },
               "guid":"900a6a99-a149-468d-bc9f-a287027b5b50"
            },
            {  
               "element":"custom-message",
               "components":{  
                  "label":{  
                     "content":"Message"
                  },
                  "message":{  
                     "name":"5[custom-message][message]",
                     "type":"text",
                     "value":"Rhrwth3rht3r . ",
                     "required":"",
                     "integration_mapping":""
                  },
                  "help_text":{  
                     "content":""
                  },
                  "wf_mapping":"",
                  "visibility":{  
                     "text":true,
                     "email":true,
                     "pager":false,
                     "fax":true
                  }
               },
               "guid":"b7ab0012-97f9-4bce-a42c-e78de69151cf"
            },
            {  
               "element":"script",
               "components":{  
                  "paragraph":{  
                     "content":"Thank you so much for calling. Have a wonderful day."
                  }
               },
               "guid":"d4a869be-f31e-475c-822a-aab0b57a5152"
            }
         ],
         "faqs":[  
         ]
      },
      {  
         "next_section":"3",
         "patch_number":"",
         "patch_ext":"",
         "elements":[  
            {  
               "element":"greeting",
               "components":{  
                  "paragraph":{  
                     "content":"Good morning."
                  }
               },
               "guid":"63c53b11-261b-472c-a4e4-347e16e38d6b"
            },
            {  
               "element":"notes",
               "components":{  
                  "paragraph":{  
                     "content":""
                  }
               },
               "guid":"d3cd55c9-8bd5-4f5b-b047-66b9c7c81a02"
            }
         ],
         "faqs":[  
         ]
      },
      {  
         "next_section":"next",
         "patch_number":"",
         "patch_ext":"",
         "elements":[  
            {  
               "element":"greeting",
               "components":{  
                  "paragraph":{  
                     "content":""
                  }
               },
               "guid":"7a19830c-7b48-48bd-8a13-c19a42a9313b"
            }
         ],
         "faqs":[  
         ]
      },
      {  
         "next_section":"next",
         "patch_number":"",
         "patch_ext":"",
         "elements":[  
            {  
               "element":"greeting",
               "components":{  
                  "paragraph":{  
                     "content":""
                  }
               },
               "guid":"a3e92b5c-ce94-4847-af1d-4b33b1372a8b"
            },
            {  
               "element":"checkbox",
               "components":{  
                  "label":{  
                     "content":"First Label"
                  },
                  "checkboxlist":[  
                     {  
                        "input":{  
                           "value":"value_0",
                           "id":"multiple_choices_answer_0"
                        },
                        "label":{  
                           "content":"Choice 1"
                        }
                     }
                  ],
                  "multiple_choices_answer":{  
                     "value":[  
                        "value_0"
                     ],
                     "name":"1[checkbox][multiple_choices_answer]",
                     "required":"",
                     "integration_mapping":""
                  },
                  "help_text":{  
                     "content":""
                  },
                  "wf_mapping":"",
                  "visibility":{  
                     "text":true,
                     "email":true,
                     "pager":false,
                     "fax":true
                  }
               },
               "guid":"9ddd7d35-1720-4270-84af-94d765e69b95"
            },
            {  
               "element":"checkbox",
               "components":{  
                  "label":{  
                     "content":"Second Label"
                  },
                  "checkboxlist":[  
                     {  
                        "input":{  
                           "value":"value_0",
                           "id":"multiple_choices_answer_0"
                        },
                        "label":{  
                           "content":"Choice 2"
                        }
                     }
                  ],
                  "multiple_choices_answer":{  
                     "value":[  
                        "value_0"
                     ],
                     "name":"2[checkbox][multiple_choices_answer]",
                     "required":"",
                     "integration_mapping":""
                  },
                  "help_text":{  
                     "content":""
                  },
                  "wf_mapping":"",
                  "visibility":{  
                     "text":true,
                     "email":true,
                     "pager":false,
                     "fax":true
                  }
               },
               "guid":"610cd6aa-b152-43f1-810a-b136340104b4"
            }
         ],
         "faqs":[  
         ]
      },
      {  
         "next_section":"next",
         "patch_number":"",
         "patch_ext":"",
         "elements":[  
            {  
               "element":"greeting",
               "components":{  
                  "paragraph":{  
                     "content":""
                  }
               },
               "guid":"96682825-e59a-4f27-ade4-835e95ae6bde"
            },
            {  
               "element":"sms-attachments",
               "components":{  
                  "label":{  
                     "content":"Cell Number to send SMS"
                  },
                  "phone":{  
                     "name":"1[sms-attachments][phone]",
                     "type":"text",
                     "value":"",
                     "required":"false",
                     "integration_mapping":"",
                     "expire_mms_link":"24"
                  },
                  "help_text_1":{  
                     "content":"Ask the caller for their mobile number.  Inform them that you will be sending them a text message and request that they reply to message with images."
                  },
                  "help_text_2":{  
                     "content":"Ask the caller for their mobile number.  Inform them that you will be sending them a text message and request that they reply to message with images."
                  },
                  "sms_content":"Please reply to this text message and attach images.",
                  "wf_mapping":"",
                  "visibility":{  
                     "text":true,
                     "email":true,
                     "pager":false,
                     "fax":true
                  }
               },
               "guid":"7d497fa2-dd8c-42c0-ae00-00db51be9fdb"
            }
         ],
         "faqs":[  
         ]
      },
      {  
         "next_section":"next",
         "patch_number":"",
         "patch_ext":"",
         "elements":[  
            {  
               "element":"greeting",
               "components":{  
                  "paragraph":{  
                     "content":""
                  }
               },
               "guid":"fc612986-b3e8-4b87-b56f-4c0288c8f4a7"
            },
            {  
               "element":"custom-date-time",
               "components":{  
                  "label":{  
                     "content":"Schedule Date & Time"
                  },
                  "help_text":{  
                     "content":""
                  },
                  "event_closed":{  
                     "content":"The event has been closed. Please contact main number for more information."
                  },
                  "sched_type":{  
                     "value":"business_hours"
                  },
                  "business_hours":{  
                     "mon":{  
                        "from":"09:00 am",
                        "to":"05:00 pm"
                     },
                     "tue":{  
                        "from":"09:00 am",
                        "to":"05:00 pm"
                     },
                     "wed":{  
                        "from":"09:00 am",
                        "to":"05:00 pm"
                     },
                     "thu":{  
                        "from":"09:00 am",
                        "to":"05:00 pm"
                     },
                     "fri":{  
                        "from":"09:00 am",
                        "to":"05:00 pm"
                     },
                     "sat":{  
                        "from":"closed",
                        "to":"closed"
                     },
                     "sun":{  
                        "from":"closed",
                        "to":"closed"
                     }
                  },
                  "event_hours":[  
                     {  
                        "date":"10\/12\/2017",
                        "from":"08:00 am",
                        "to":"06:00 pm"
                     }
                  ],
                  "time_interval":{  
                     "number":"15",
                     "measure":"minute"
                  },
                  "date":{  
                     "name":"1[custom-date-time][date]",
                     "value":"",
                     "required":"",
                     "integration_mapping":""
                  },
                  "day":{  
                     "name":"1[custom-date-time][day]",
                     "value":"",
                     "required":""
                  },
                  "month":{  
                     "name":"1[custom-date-time][month]",
                     "value":"",
                     "required":""
                  },
                  "year":{  
                     "name":"1[custom-date-time][year]",
                     "value":"",
                     "required":""
                  },
                  "time":{  
                     "name":"1[custom-date-time][time]",
                     "value":"",
                     "required":"",
                     "integration_mapping":""
                  },
                  "dateTime":{  
                     "name":"1[custom-date-time][dateTime]",
                     "value":"",
                     "integration_mapping":""
                  },
                  "visibility":{  
                     "text":true,
                     "email":true,
                     "pager":false,
                     "fax":true
                  }
               },
               "guid":"5424f1e1-4523-4ce4-acb4-a3637e81eac7"
            }
         ],
         "faqs":[  
         ]
      }
   ],
   "feedback":{  
      "comment":"",
      "call_ended":{  
         "reason":"",
         "comment":""
      }
   },
   "date_created":1507843214,
   "last_modified":1507843316.804022,
   "status":"published",
   "published":true,
   "_callsid":"CAa29521566c53e32d1ca865e841fa1a8e",
   "visited_arr":[  
      0
   ]
}

...我准备接收一个 JSON对象:

改造:

   @GET("android/v7/get_script")
    Call<List<JSONObject>> getCallDetails(@Query("call_id") String callId, @Query("call_status") String callStatus,  @Query("build") Boolean preBuild);

和:

 private void getCallDetails() {
        mSelectedCall.build = false;
        App.api.getCallDetails(mSelectedCall.idx, mSelectedCall.caller_status, mSelectedCall.build).enqueue(new API.SimpleCallback<List<JSONObject>>() {
            @Override
            public void onResponse(List<JSONObject> elements) {

...
            }

            @Override
            public void onFailure(Call<List<JSONObject>> call, Throwable t) {
                super.onFailure(call, t);

            }

        });
    }

但是我收到一条错误消息:

预期 BEGIN_ARRAY 但 BEGIN_OBJECT 在第 1 行第 2 列路径 $

...我不知道为什么。

如有任何建议,我们将不胜感激!

原因是你上面贴的JSON是一个对象,不是数组。

您可以将 JSON 更改为数组或接收对象 而不是数组

您可以在下面的 link 中检查 JSON 是一个对象: Click here

写得不错!

如错误所述,您需要此行中的列表

 Call<List<JSONObject>> getCallDetails

您应该删除一个列表对象,因为您的 json 不是以 [ ] 开头的。

Call<JSONObject> getCallDetails

之前的答案可能是正确的,但如果您确实需要一个 JSONObject 列表,您的 api 应该 return 类似于:

[{ },...]