J'essaye de convertir une chaîne en JSONObject. Voici mon code:
org.json.JSONException: Value {"vin" : "VIN5", "brand" : "Toyota", "model" : "Innova", "year" : "2017", "color" : "Red", "modelCode" : "1234", "type" : "M", "countryCode" : "JP", "isConnected" : "true", "isActive" : "true"} of type java.lang.String cannot be converted to JSONObject at org.json.JSON.typeMismatch(JSON.java:111) at org.json.JSONObject.<init>(JSONObject.java:159) at org.json.JSONObject.<init>(JSONObject.java:172) at com.examples.demo.VehicleStepDefs.submitValidVehicleRequest(VehicleStepDefs.java:43) at ?.Given vehicle json for VehicleService(Vehicle.feature:8)
Le fichier Vehicle.feature contient:
@Given("^vehicle json for VehicleService$") public void submitValidVehicleRequest(String vehicleJson) throws JSONException { JSONObject obj = new JSONObject(vehicleJson); request = given().and() .header("Content-Type", MediaType.APPLICATION_JSON) .accept(ContentType.JSON) .body(obj); request.then().log().all(); }
VehicleStepDefs contient:
Scenario: Create a vehicle with valid json request Given vehicle json for VehicleService """ "{\"vin\" : \"VIN5\", \"brand\" : \"Toyota\", \"model\" : \"Innova\", \"year\" : \"2017\", \"color\" : \"Red\", \"modelCode\" : \"1234\", \"type\" : \"M\", \"countryCode\" : \"JP\", \"isConnected\" : \"true\", \"isActive\" : \"true\"}" """ When performing POST on VehicleService url /add Then VehicleService should return status code 200
3 Réponses :
Vérifiez vos importations.
Voici le code en cours d'exécution:
import org.json.JSONException; import org.json.JSONObject; public class TestJson { public static void main(String[] args) { try { JSONObject obj = new JSONObject("{\"vin\" : \"VIN5\", \"brand\" : \"Toyota\", \"model\" : \"Innova\", \"year\" : \"2017\", \"color\" : \"Red\", \"modelCode\" : \"1234\", \"type\" : \"M\", \"countryCode\" : \"JP\", \"isConnected\" : \"true\", \"isActive\" : \"true\"}"); System.out.println(obj.get("model")); } catch (JSONException e) { e.printStackTrace(); } } }
Sortie: Innova
J'utilise le dernier jar org.json json-20190722.jar
Et je peux imprimer la chaîne json sans aucun problème, vous pouvez consulter mon code:
public static void main(String[] args) { String str = "{\"vin\" : \"VIN5\", \"brand\" : \"Toyota\", \"model\" : \"Innova\", \"year\" : \"2017\", \"color\" : \"Red\", \"modelCode\" : \"1234\", \"type\" : \"M\", \"countryCode\" : \"JP\", \"isConnected\" : \"true\", \"isActive\" : \"true\"}"; JSONObject obj = new JSONObject(str); System.out.println(obj.getString("brand")); // this also works when we are not sure of the return type of the resultant object System.out.println(obj.get("brand")); }
J'écris des tests de concombre. Je dois transmettre Json dans le corps de la demande. J'ai mis à jour la question avec plus de détails
Il semble que la chaîne de réponse a été encodée avec un autre type (comme UTF-8
avec BOM ). Essayez d'ajouter l'extrait de code suivant, puis convertissez-le à nouveau.
vehicleJson = vehicleJson.substring(vehicleJson.indexOf("{"), vehicleJson.lastIndexOf("}") + 1); JSONObject obj = new JSONObject(vehicleJson); ...
Maintenant, j'obtiens l'exception: org.json.JSONException: Valeur littérale attendue au caractère 1 de {\ "vin \" ...