Java JSONObject文本必须以';{';在1[字符2第1行]处,通过RestTemplate调用NEWSAPI
我得到的错误是“JSONObject文本必须在1[字符2第1行]以“{”开头” 在使用Spring的RestTemplate调用NewsAPI时 下面是我用来获取响应的代码片段。我能够获取响应字符串,但无法通过org.JSON类解析它Java JSONObject文本必须以';{';在1[字符2第1行]处,通过RestTemplate调用NEWSAPI,java,rest,Java,Rest,我得到的错误是“JSONObject文本必须在1[字符2第1行]以“{”开头” 在使用Spring的RestTemplate调用NewsAPI时 下面是我用来获取响应的代码片段。我能够获取响应字符串,但无法通过org.JSON类解析它 import java.util.Arrays; import org.json.JSONArray; import org.json.JSONObject; import org.springframework.context.annotation.Bean;
import java.util.Arrays;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.context.annotation.Bean;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;
public class APITest {
final private static String NEWSAPI_URI="https://newsapi.org/v2/top-headlines";
final private static String API_TOKEN_VAL=“*****************”;
final private static String API_TOKEN="apiKey";
final private static String COUNTYRY="Country";
final private static String COUNTYRY_VAL="US";
public static void main(String[] args) {
RestTemplate restTemplate=new RestTemplate();
UriComponentsBuilder builder=UriComponentsBuilder.fromHttpUrl(NEWSAPI_URI)
.queryParam(API_TOKEN, API_TOKEN_VAL)
.queryParam(COUNTYRY, COUNTYRY_VAL)
;
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
HttpEntity<String> entity = new HttpEntity<String>("parameters", headers);
ResponseEntity<String> responseEntity=restTemplate.exchange(builder.toUriString(), HttpMethod.GET, entity, String.class);
//parsing responseEntity
HttpStatus httpStatus=responseEntity.getStatusCode();
String responseString=responseEntity.getBody();
System.out.println(responseString);
if(httpStatus.is2xxSuccessful()) {
JSONObject jsonObject=new JSONObject(responseString.trim());
JSONArray articles=jsonObject.getJSONArray("articles");
//System.out.println(articles.getJSONObject(0).get("content"));
for(int it=0;it<articles.length();it++) {
JSONObject articleObject=articles.getJSONObject(it);
System.out.println(articleObject.getString("content"));
}
}
}
@Bean
public RestTemplate getRestTemplate() {
return new RestTemplate();
}
}
导入java.util.array;
导入org.json.JSONArray;
导入org.json.JSONObject;
导入org.springframework.context.annotation.Bean;
导入org.springframework.http.HttpEntity;
导入org.springframework.http.HttpHeaders;
导入org.springframework.http.HttpMethod;
导入org.springframework.http.HttpStatus;
导入org.springframework.http.MediaType;
导入org.springframework.http.ResponseEntity;
导入org.springframework.web.client.rest模板;
导入org.springframework.web.util.UriComponentsBuilder;
公共类蜂鸟测试{
最终私有静态字符串NEWSAPI_URI=”https://newsapi.org/v2/top-headlines";
最终私有静态字符串API_TOKEN_VAL=“*******************”;
最终私有静态字符串API_TOKEN=“apiKey”;
最终私有静态字符串COUNTYRY=“Country”;
最终私有静态字符串COUNTYRY\u VAL=“US”;
公共静态void main(字符串[]args){
RestTemplate RestTemplate=新RestTemplate();
UriComponentsBuilder=UriComponentsBuilder.fromHttpUrl(新闻API_URI)
.queryParam(API_令牌,API_令牌)
.queryParam(COUNTYRY,COUNTYRY_VAL)
;
HttpHeaders=新的HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
HttpEntity=新的HttpEntity(“参数”,标题);
ResponseEntity ResponseEntity=restTemplate.exchange(builder.toUriString(),HttpMethod.GET,entity,String.class);
//解析响应
HttpStatus HttpStatus=responseEntity.getStatusCode();
字符串responseString=responseEntity.getBody();
系统输出打印LN(响应预算);
if(httpStatus.is2xxsuccesful()){
JSONObject=newJSONObject(responseString.trim());
JSONArray articles=jsonObject.getJSONArray(“articles”);
//System.out.println(articles.getJSONObject(0.get(“content”));
对于(int it=0;它不是简单地抛出代码,而是为了帮助您更好地解释到目前为止您已经尝试了什么,并尝试使您的问题更清楚。帮助可能更好,因为我们知道的更多。您是否也可以发布responseString
,以便我们知道您是否正确解析它。响应字符串包含��{“status”:“ok”,“totalResults”:37,“articles”:[{“source”:{“id”:null,“name”:“Stltoday.com”}….通过添加以下行..headers.set(“接受字符集”,“utf-8;”)来解决问题;