Skip to content
Advertisement

Gson returns Json object with `=` instead of `:` [closed]

Here is my code:

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)
);

The assertion fails because:

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}]

Why does my expected object contain = instead of :? I even tried it with the actual json string hardcoded, but I get the same result:

Advertisement

Answer

By default, the toString() is mapping ':' to '='. To resolve this, use get the jsonString from myJsonObject and pass it to gson.fromJson(). You can use ObjectMapper for this, or Gson itself.

with objectMapper:

objectMapper.writeValueAsString(myJsonObject);

with Gson:

gson.toJson(myJsonObject);
5 People found this is helpful
Advertisement