2012-07-04 4 views
0

I가이 형식으로 JSON 문자열을 반환하는 PHP Webservice를 :어떻게 JSON있는 NSDictionary에서 개체를 만들 수

#import <Foundation/Foundation.h> 
#import <MapKit/MapKit.h> 

@interface PCoordenada : NSObject 

@property (nonatomic) CLLocationCoordinate2D *punto; 
@property (nonatomic,strong) NSString *nombre; 
@end 
:

내 프로젝트에서
[{"latitud":"37.995914","longitud":"-1.139705","nombre":"Miguel de 
Unamuno"},{"latitud":"37.995433","longitud":"-1.140143","nombre":"Calle 
Pina"},{"latitud":"37.99499","longitud":"-1.140361","nombre":"Calle 
Moncayo"},{"latitud":"37.993918","longitud":"-1.139392","nombre":"Calle 
Moncayo2"},{"latitud":"37.994588","longitud":"-1.138543","nombre":"Calle 
Salvador de Madriaga"}] 

, 나는 다음 구조의 사용자 정의 클래스를 가지고 궁금

#import <UIKit/UIKit.h> 
#import "PCoordenada.h" 

@interface TestViewController : UIViewController 

@property (nonatomic,strong) NSData * HTTPResponse; 
@property (nonatomic,strong) NSDictionary * dic; 
@property (nonatomic,strong) NSMutableArray *arrayCoord; 
@property (nonatomic,strong) PCoordenada *coor; 

-(IBAction)GetDataFrom:(id)sender; 

@end 

: 미안 메인 앱 다른 클래스를 사용하여 다음

, JSON 문자열 정보를 포함하는 PCoordenada 객체 배열을 만드는 방법.

누구든지 나를 도울 수 있습니까?

+0

[iOS의 기본 JSON 지원?] (http://stackoverflow.com/questions/3562478/native-json-support-in-ios) – Monolo

답변

4

: 사전에

덕분에이 작업을 수행합니다 : 객체의 NSArray를로 JSON을 넣어 것입니다

NSData *theData = [NSData dataWithContentsOfURL:[NSURL URLWithString:YOUR_URL]]; 
NSArray *arrRequests = [NSJSONSerialization JSONObjectWithData:theData options:NSJSONReadingMutableContainers error:nil]; 

합니다. 이러한 각 개체는 NSDictionary입니다. 그렇다면 각각의 NSDictionary를 얻기 위해 NSArray를 반복하면됩니다.

//now let's loop through the array and generate the necessary annotation views 
for (int i = 0; i<= arrRequests.count - 1; i++) { 
    //now let's dig out each and every json object 
    NSDictionary *dict = [arrRequests objectAtIndex:i];} 

당신은 루프가있는 NSDictionary의 핵심으로 JSON 속성을 보유에서 얻을 각 NSDictionary에 : 그것은 더 나은 성능을 위해 JSON을 읽을 때 멀티 스레딩을 사용하는 것이 좋습니다 또한

NSString *address = [NSString stringWithString:[dict objectForKey:@"Address"]]; 
+0

니스! 이것은 더 많은 문제없이 내 문제를 해결합니다. 감사!! – miguelmsa1

1

입니다. This 문서는 사용법을 따르기 매우 쉽습니다. 나는 읽기를 권장합니다.

관련 문제