2012-06-22 1 views
0

JSON 데이터를 구문 분석하려고합니다. 데이터는 내부에 객체가있는 Array입니다.iOS5 : NSJSONSerialization을 사용하여 JSON에서 값을 읽을 수 없습니다.

["{content:Airfare}", 
"{content:Dues \/ Subscriptions}", 
"{content:Education \/ Training}", 
"{content:Entertainment}", 
"{content:GS-OCWD}", 
"{content:GS-OCWE}", 
"{content:GS-Shift A}", 
"{content:GS-Shift B}", 
"{content:GS-Shift C}", 
"{content:Ground Transportation}", 
"{content:Legal Fees}", 
"{content:Lodging}", 
"{content:Meals}", 
"{content:Mileage}", 
"{content:Office Supplies}", 
"{content:Other Expenses}", 
"{content:Prof. Dues & Memberships}", 
"{content:Rental Car}", 
"{content:Telephone}", 
"{content:Telephone \/ Internet}", 
"{content:Tolls \/ Parking}"] 

라인 NSLog(@"Item: %@", [item objectForKey:@"content"]);이 응용 프로그램 충돌을 실행하고하면이 내하는 .m 파일

NSError *error = nil; 
    NSData *jsonData = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://localhost:8080/de.vogella.jersey.final/rest/notes"]];  


    NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData: jsonData options: NSJSONReadingMutableContainers error: &error]; 

    if (!jsonArray) { 
     NSLog(@"Error parsing JSON: %@",error); 
    } else { 
     for(NSDictionary *item in jsonArray) { 
      NSLog(@"Item: %@", [item objectForKey:@"content"]); 
      [_identificationTypes1 addObject:item]; 
     } 

    } 

에서 JSON 배열을 구문 분석에 대한 코드는 다음과 같습니다 이 내가 URL에서 얻을 JSON 배열입니다 [__NSCFString objectForKey:]: unrecognized selector 오류가 발생합니다. 키 내용을 읽을 수 없습니다. 줄을 NSLog(@"Item: %@", item);으로 변경하면 {content:Airfare}과 같은 값을 모두 볼 수 있습니다. Airfare 가치가 필요합니다. 누군가 나를 도울 수 있습니까

이것은 JSON을 생성하는 코드입니다. 저지와 JAVA를 사용하고 있습니다. URL에서 JSON 형식을 도울 수 있습니까?

public JSONArray getAllNotes() 
    { 
     PreparedStatement prepStmt = null; 
     List<Note> notes = new ArrayList<Note>(); 
     try { 
      String cSQL = "SELECT EXPENDITURE_TYPE FROM PA_ONLINE_EXPENDITURE_TYPES_V; 
      prepStmt = connection.prepareStatement(cSQL); 
      ResultSet result = prepStmt.executeQuery(); 
      while (result.next()) 
      { 
       Note note = new Note(); 
       //note.setNoteId(result.getInt(1)); 
       note.setContent(result.getString(1)); 
       //note.setCreatedDate(new java.util.Date(result.getDate(3).getTime())); 
       notes.add(note); 
      } 
      return new JSONArray(notes); 
     } catch (SQLException e) { 
      e.printStackTrace(); 
      prepStmt = null; 
      return null; 
     } 
    }      

이는 POJO 방법입니다 :

@GET 

@Produces({MediaType.APPLICATION_JSON}) 
     public JSONArray getNotes() { 
      return dao.getAllNotes(); 
     }  

답변

0

귀하의 JSON 잘못 :

@Override 
     public String toString() { 
      //return "{Content:"+content+"}" ; 
      return "ExpType [content=" + content + "]"; 
     }  

이것은 DAO 메소드를 호출하는 방법이 내 DAO 코드입니다. 그것은 문자열의 배열 일 뿐이므로이 오류가 발생합니다. 그것이 진정으로 좋아야하는 것은 :

[{"content":"Airfare"}, 
{"content":"Dues \/ Subscriptions"}, 
{"content":"Education \/ Training"}, 
... etc] 
+0

올바른 형식을 어떻게 만듭니 까? 이 새로운 것을 좀 도와주세요. – user1342592

+0

제가 말하고자하는 것은 문제가 귀하의 URL에서 얻은 응답에 있다는 것입니다. 귀하의 경우에는'http : // localhost : 8080/de.vogella.jersey.final/rest/notes'에서 얻은 JSON 형식이 범인입니다. objc 코드에 문제가 없습니다. – Alladinian

+0

URL에서 JSON 형식으로 나를 도울 수 있습니까? 이것은 내 DAO 코드입니다. – user1342592