2017-04-25 77 views
0

我得到一个JSON数组作为响应从其余webservice,我想遍历它来获得它的各种属性。有多个json数组具有相同的名称,只有属性值和不同。为此,我尝试了各种代码片段。我已经提到了所有我尝试过的代码片断,并带有错误信息。getJSONObject(int)是未定义的类型JSONArray

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

web服务的响应有点像

[ 
{"mutualFund":{"fundCode":"xyz","fundName":"123","isin":"IE000"}}, 
{"mutualFund":{"fundCode":"xyz","fundName":"123","isin":"xyz"}}, 
{"mutualFund":{"fundCode":"xyz","fundName":"123","sedol":"WB1"}} 
] 

回答

0

您不必使用分析器,监守JSON数组构造函数采用字符串作为放慢参数

下载以下JSON lib,使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(); 
    } 
+0

构造函数JSONArray(String)是未定义的错误是c oming –

+0

什么是你使用的lib? json有很多libs –

+0

org.json.simple.JSONArray; –

相关问题