2016-08-10 2 views
0

히 내가 자바 포함하는 다음과 같은 구조에서 .json 파일을 읽는 데 문제가 있어요에서 읽기 는 .json 파일

{"id":"1", "name":"A", "address":{"plot no":"22", "street":"road"}} 
{"id":"2", "name":"A", "address":{"plot no":"22", "street":"road"}} 
{"id":"3", "name":"A", "address":{"plot no":"22", "street":"road"}} 
{"id":"4", "name":"A", "address":{"plot no":"22", "street":"road"}} 

나는 등 10,000 기록을 가지고있다. 나는 구조를 바꿀 수 없다. 나는 그것을 읽고 "주소"에 대한 처리를하고 싶다. 나는 그것을 읽고 효율적으로 주소를 가져 오는 효율적인 방법이 필요하다. 어떤 제안?

답변

0

yourJsonObject는 java에서 json 개체로 추출 된 json 파일입니다. 그리고 이것 :

JSONObject jso = yourJsonObject.getJSONObject("address") ; 

당신의 "주소"부분을 json 객체로 추출합니다. 그런 다음 모든 고전적 처리를 수행 할 수 있습니다.

0

JSON 간단한 라이브러리를 사용할 수 있습니다.이 예제로 아이디어를 얻으시기 바랍니다.

JSONParser parser = new JSONParser(); 
    JSONArray a = (JSONArray) parser.parse(new FileReader("file name")); 

    for (Object o : a){ 
     JSONObject person = (JSONObject) o; 

     String name = (String) person.get("name"); 
     System.out.println(name); 

     String city = (String) person.get("city"); 
     System.out.println(city); 

     String job = (String) person.get("job"); 
     System.out.println(job); 

    JSONArray cars = (JSONArray) jsonObject.get("cars"); 

    for (Object c : cars) 
     { 
     System.out.println(c+""); 
     } 
    }