我对此有疑问,但在 Stack Overflow 上阅读了其他问题并最初解决了这些问题。现在我可以在我的客户端和服务器之间发送 JSON 并在客户端中从这个 JSON 创建对象。但是,我不断收到一个特定对象的此错误:
client.restaurant = gson.fromJson(obj.get("restaurant"), Restaurant.class); // works
client.postcodes = gson.fromJson(obj.get("postcodes"), new TypeToken<ArrayList<Postcode>>(){}.getType()); // works
client.orders = gson.fromJson(obj.get("orders"), new TypeToken<ArrayList<Order>>(){}.getType()); // java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at path $[0].dishes.updateListeners
所有 JSON 根据https://jsonlint.com都是有效的。我不知道如何找到有关此问题的更多信息,但我尝试的一件事是我提取了 JSONArrayList<Order> orders并尝试手动解析它:
JsonObject testOrder = parser.parse("{\n" +
" \"orders\": [\n" +
" {\n" +
" \"user\": {\n" +
" \"userName\": \"Admin\",\n" +
" \"password\": \"password\",\n" +
" \"address\": \"University Road\",\n" +
" \"postcode\": {\n" +
" \"postcodeName\": \"SO17 1BJ\",\n" +
" \"latLong\": {\n" +
" \"lon\": 0.0,\n" +
" \"lat\": 0.0\n" +
" },\n" +
" \"distance\": 0,\n" +
" \"updateListeners\": []\n" +
" },\n" +
" \"updateListeners\": []\n" +
" },\n" +
" \"dishes\": {\n" +
client.orders = gson.fromJson(testOrder, new TypeToken<ArrayList<Order>>(){}.getType()));
但这也不起作用(错误略有不同-- Expected BEGIN_ARRAY but was BEGIN_OBJECT)。
喵喔喔
相关分类