Gson返回带有'='而不是':'的Json对象[已关闭]

mspsb9vt  于 2022-11-06  发布在  其他
关注(0)|答案(1)|浏览(184)

**已关闭。**此问题需要debugging details。当前不接受答案。

编辑问题以包含desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem。这将有助于其他人回答问题。
去年关闭了。
Improve this question
下面是我的代码:

assertEquals(
    gson.fromJson("[{\"vehicle_side\":\"driver\",\"vehicle_occupant_role\":\"driver\",\"vehicle_window\":\"front_right\",\"window_status\":10.0}]", List.class),           
    gson.fromJson(myJsonObject, List.class)
);

判断提示失败的原因是:

Expected :[{vehicle_window=front_right, vehicle_occupant_role=driver, window_status=10.0, vehicle_side=driver}]
Actual   :[{"vehicle_side":"driver","vehicle_occupant_role":"driver","vehicle_window":"front_right","window_status":10.0}]

为什么我期望的对象包含=而不是:?我甚至用硬编码的实际json字符串尝试了一下,但得到了相同的结果:

goucqfw6

goucqfw61#

默认情况下,toString()':'Map到'='。若要解决此问题,请使用从myJsonObject获取jsonString并将其传递给gson.fromJson()。您可以使用ObjectMapper或Gson本身来执行此操作。
使用对象Map器:

objectMapper.writeValueAsString(myJsonObject);

与Gson:

gson.toJson(myJsonObject);

相关问题