簡體   English   中英

JSONException 字符串無法轉換為 JSONObject

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM