![](/img/trans.png)
[英]JSONException:<String>cannot be converted to JSONObject with helps of JSON Array
[英]JSONException String cannot be converted to JSONObject
我正在尝试将字符串转换为 JSONObject。 这是我的代码:
JSONObject obj = new JSONObject(str);
Vehicle.feature 文件包含:
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 包含:
@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();
}
我的错误是这样的:
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)
我究竟做错了什么?
检查您的进口。
下面是运行代码:
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();
}
}
}
输出:创新
我正在使用最新的 org.json jar json-20190722.jar
而且我可以毫无问题地打印 json 字符串,您可以参考我的代码:
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"));
}
当我尝试改变
System.out.println(obj.getString("brand"));
到
System.out.println(obj.getJSONArray("brand"));
我得到了这个例外
线程“main” org.json.JSONException 中的异常:JSONObject[“brand”] 不是 JSONArray。 在 org.json.JSONObject.getJSONArray(JSONObject.java:752) 在 com.christmas.Main.main(Main.java:14)
这清楚地提到结果不是JsonArray类型。
最好发布您的逻辑并完成 stacktrace 。
似乎响应字符串是用另一种类型编码的(例如带有BOM 的UTF-8
)。 尝试添加以下代码片段,然后再次转换。
vehicleJson = vehicleJson.substring(vehicleJson.indexOf("{"), vehicleJson.lastIndexOf("}") + 1);
JSONObject obj = new JSONObject(vehicleJson);
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.