I am trying to convert string to JSONObject. This is my code:
JSONObject obj = new JSONObject(str);
Vehicle.feature file contains :
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
VehicleStepDefs contains:
@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();
}
My error looks like this :
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)
What am I doing wrong?
Check your imports.
Here is the running code:
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();
}
}
}
Output: Innova
I am using the latest org.json jar json-20190722.jar
And i am able to print the json string without any issues, you can refer my 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"));
}
When i try changing the
System.out.println(obj.getString("brand"));
to
System.out.println(obj.getJSONArray("brand"));
I got this exception
Exception in thread "main" org.json.JSONException: JSONObject["brand"] is not a JSONArray. at org.json.JSONObject.getJSONArray(JSONObject.java:752) at com.christmas.Main.main(Main.java:14)
which is clearly mentioning that the result is not of JsonArray type.
It is better you post your logic and complete stacktrace .
It seems the response string was encoded with another type (such as UTF-8
with BOM ). Try adding following code snippet then convert it again.
vehicleJson = vehicleJson.substring(vehicleJson.indexOf("{"), vehicleJson.lastIndexOf("}") + 1);
JSONObject obj = new JSONObject(vehicleJson);
...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.