是的,我知道這個問題已經討論了幾次,但我沒有設法解決我的問題。無法讀取JSON:無法識別的字段(...),未標記爲可忽略
所以我從一個http請求使用org.springframework.web.client.RestTemplate得到一個JSONObject:
JSONObject j = RestTemplate.getForObject(url, JSONObject.class);
但我得到這個錯誤:
Exception in thread "main" org.springframework.http.converter.HttpMessageNotReadableException: Could not read JSON: Unrecognized field "uri" (Class org.json.JSONObject), not marked as ignorable
at [Source: [email protected]6c5f; line: 2, column: 12] (through reference chain: org.json.JSONObject["uri"]); nested exception is org.codehaus.jackson.map.exc.UnrecognizedPropertyException: Unrecognized field "uri" (Class org.json.JSONObject), not marked as ignorable
at [Source: [email protected]6c5f; line: 2, column: 12] (through reference chain: org.json.JSONObject["uri"])
at org.springframework.http.converter.json.MappingJacksonHttpMessageConverter.readJavaType(MappingJacksonHttpMessageConverter.java:181)
at org.springframework.http.converter.json.MappingJacksonHttpMessageConverter.read(MappingJacksonHttpMessageConverter.java:173)
at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:94)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:517)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:472)
at org.springframework.web.client.RestTemplate.getForObject(RestTemplate.java:237)
我想要訪問Rest-Api並且Json對象可以具有不同的字段名稱。我已經試過@JsonIgnoreProperties(ignoreUnknown=true)
。但這是行不通的...
如何將響應轉換爲JSONObject?
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(
DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
我讀過關於這個,但我如何使用readValue()方法? – Maik 2014-10-17 10:44:23
好吧,沒關係我想通了,但現在我得到一個'在線程中的異常「主」org.codehaus.jackson.JsonParseException:意外的字符('H'(代碼104)):預期一個有效的值(數字,字符串,數組,對象,'true','false'或'null')' – Maik 2014-10-17 11:04:54
對不起,有很多答案..我正在研究它,並讓它運行。現在感謝! – Maik 2014-10-17 11:28:22