JSONException字符串无法转换为JSONObject

问题描述 投票:0回答:3

我正在尝试将字符串转换为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)

我在做什么错?

java json string cucumber-java jsonexception
3个回答
1
投票

检查您的进口。

这里是运行代码:

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

}

输出:Innova


0
投票

我正在使用最新的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”));

to

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。


0
投票

似乎响应字符串是用其他类型编码的。尝试添加以下代码段。

© www.soinside.com 2019 - 2024. All rights reserved.