Arrays 对于类型JSONArray,getJSONObject(int)未定义
我从RESTWebService得到一个JSON数组作为响应,我想对它进行迭代以获得它的各种属性。有多个json数组具有相同的名称,只有属性值和不同的值。为此,我尝试了各种代码片段。我已经提到了我所有尝试过的代码片段以及我得到的错误Arrays 对于类型JSONArray,getJSONObject(int)未定义,arrays,json,Arrays,Json,我从RESTWebService得到一个JSON数组作为响应,我想对它进行迭代以获得它的各种属性。有多个json数组具有相同的名称,只有属性值和不同的值。为此,我尝试了各种代码片段。我已经提到了我所有尝试过的代码片段以及我得到的错误 ResponseEntity<String> response = restTemplate.exchange("xyz.com", HttpMethod.GET, entity, String.class); JS
ResponseEntity<String> response = restTemplate.exchange("xyz.com",
HttpMethod.GET, entity, String.class);
JSONParser parser=new JSONParser();
System.out.println("Response is"+response.getBody());
try{
//JSONObject outerObject = (JSONObject)parser.parse(response.getBody()); Class Cast Exception
//JSONObject jsonObj = new JSONObject(response.getBody()); jsonobject must begin with {
JSONArray jsonArray = (JSONArray) parser.parse(response.getBody());
for (int i = 0; i < jsonArray.size(); i++)
{
/*JSONObject object = jsonArray.getJSONObject(i);*/// getJSONObject(int) is undefined for the type JSONArray
}
}catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
您不必使用解析器,因为json数组构造函数将字符串作为参数 下载以下内容以使json易于解析
ResponseEntity<String> response = restTemplate.exchange("xyz.com",
HttpMethod.GET, entity, String.class);
System.out.println("Response is"+response.getBody());
try{
JSONArray outerObject = new JSONArray(response.getBody());
for(JSONObject object: outerObject)
{
// Your Logic
}
}catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ResponseEntity response=restemplate.exchange(“xyz.com”),
HttpMethod.GET、实体、字符串.class);
System.out.println(“Response is”+Response.getBody());
试一试{
JSONArray outerObject=newjsonarray(response.getBody());
for(JSONObject对象:outerObject)
{
//你的逻辑
}
}捕获(解析异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
构造函数JSONArray(字符串)未定义错误为comingorg.json.simple.JSONArray;请不要使用simple稍等片刻,我将为您提供正确的lib。这是我在pom.xml com.googlecode.json-simple json simple 1.1中的依赖项。
ResponseEntity<String> response = restTemplate.exchange("xyz.com",
HttpMethod.GET, entity, String.class);
System.out.println("Response is"+response.getBody());
try{
JSONArray outerObject = new JSONArray(response.getBody());
for(JSONObject object: outerObject)
{
// Your Logic
}
}catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}