회사 목록을 인쇄하려고합니다.JSON 배열 구문 분석
내 JSON 개체 :
가{
"Name": "crunchify.com",
"Author": "App Shah",
"Company List": [
"Compnay: eBay",
"Compnay: Paypal",
"Compnay: Google"
]
}
코드 :
는public class ProductTypeParser {
public void parseJson(JSONObject jsonObject) throws ParseException {
JSONObject object = (JSONObject) jsonObject;
String name = (String) object.get("Name");
System.out.println(name);
String age = (String) object.get("Author");
System.out.println(age);
//loop array
JSONArray msg = (JSONArray) object.get("Company List");
Iterator<String> iterator = msg.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
}
}
오류 :
SEVERE: Servlet.service() for servlet [contextServlet] in context with path [/SpringSafeHouseService2.0]
threw exception [Request processing failed; nested exception is
java.lang.ClassCastException: java.util.ArrayList cannot be cast to org.json.simple.JSONArray]
with root cause java.lang.ClassCastException: java.util.ArrayList cannot be cast to org.json.simple.JSONArray
그냥 보조 노트 : 프로덕션으로 코드를 삽입하기 전에 당신이 당신의 이름을 리팩토링해야한다. ;) -'String age = (String) object.get ("Author");'... – Thomas
오류 메시지에서 오른쪽 :'ArrayList'를'JSONArray'에 캐스트 할 수 없습니다. 이걸 어쩌면 할 수 있니? :'ArrayList msg = object.get ("Company List");' – TW80000
실제로 json 문자열을 어떻게 파싱하고 있습니까? 당신의 메소드는 실제 구문 분석을하지 않습니다. 이것은 초기 JSONObject 인스턴스를 생성 할 때 이미 완료되었습니다. – Thomas