2013-08-12 8 views
0

아래 코드를 찾으십시오. 이걸 실행하면 org.json.simple.JSONObject cannot be cast to org.json.simple.JSONArray 예외가 발생합니다. 도와주세요.Json-Simple, 캐스트 예외

{ "tsNow": 1376325485, "데이터": { "나는

public static void main(String[] args) throws Exception {  
    URL website = new URL("http://MY_TESTING_SITE"); 
    URLConnection connection = website.openConnection(); 
    BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream())); 

    StringBuilder response = new StringBuilder(); 
    String inputLine; 


    while ((inputLine = in.readLine()) != null) 
     response.append(inputLine); 

    in.close(); 

    JSONParser parser = new JSONParser(); 
    Object obj = parser.parse(response.toString()); 
    JSONObject jsonObject = (JSONObject) obj; 

    System.out.println(jsonObject.get("data")); //Works fine till here and get proper output 

    JSONArray msg = (JSONArray) jsonObject.get("data"); 


    Iterator<String> iterator = msg.iterator(); 
    while (iterator.hasNext()) { 
     System.out.println(iterator.next()); 
    } 

} 

샘플 JSON 출력은 아래 그림처럼 보이는 JSON 안에 모든 값을 인쇄하기 위해 노력하고있어 1352 ": { "for_loyal ":"1 ", "offer_image ":"http://MY_IMAGE.jpg " "상태 ":"1 ", "brand_id ":"22 ", "ID ":"1352 " "brand_image": "http://MY_BRAND_IMAGE.png", "제목": "미국에 승리 20 가족 휴가", "설명": "일부 설명", "EXPIRY_DATE": 1383018300, "BRAND_NAME": "브랜드 이름", "store_locations"[ { " CITY_ID ":"46 ", "country_id ":"9 " } ] } }, "성공 "사실 }

+0

추출 할 JSON을 넣을 수 있습니까? –

+0

"data"속성의 내용은 무엇입니까? – Bohemian

답변

0

반환되는 json이 배열 [즉, "asdf", "asdf2", "basdf"] 인 경우 java에서 json-simple로 구문 분석하면 JSONArray 객체가됩니다.

반환 된 json이 json 개체 인 경우 ("키": "값", "keyArray": [ "asdf", "asdf"]} ) JSONObject를 구문 분석합니다. keyArray 값을 얻으면 JSONArray로 구문 분석합니다.

0

귀하의 URL이 올바른 JSON 객체를 반환 확인하십시오.

응답 개체에 HTML 페이지가 들어있을 수 있습니다. json 객체가 아닙니다. Else 데이터 객체는 반환하는 json에는 없습니다.

html! = json

+0

유효한 JSON을 반환합니다. "http://jsonviewer.stack.hu/"을 사용하여 동일하게 확인했습니다. –

+0

이렇게 귀환 한 json을 확인하십시오. { "data": [ "data 1", "data 2", "data 3"] } – Prabhakaran