2 Stimmen

Wie wird das JSON-Parsing verwendet?

Ich bin ein Neuling. Ich weiß nicht, wie man JSON für Parsing verwenden. Ich bin wirklich sehr verwirrt, weil das Tutorial, das ich gelesen habe, veraltet sind, weil Apple alle APIs und Dokumente veraltet hat. das ist, was ich gefunden.

1voto

S.P. Punkte 5407

Sie können verwenden SBJSON

Sie finden den Code unter GITHUB

1voto

johne Punkte 6750

0voto

gjd Punkte 61

Sie müssen den Code eines Drittanbieters verwenden, um JSON zu parsen. Drei beliebte Bibliotheken/Frameworks sind:

Wählen Sie eine davon aus, laden Sie den Quellcode herunter und fügen Sie sie in Ihr Projekt ein.

0voto

sinh99 Punkte 3871

Sie können Ihren eigenen Rahmen dafür wie folgt verwenden

                #import <Foundation/Foundation.h>
            #import "SVProgressHUD.h"
            @protocol JsonDataDelegate <NSObject>

            @required
            - (void) JsonprocessSuccessful: (NSString *)receivedData;
            -(void) JsonprocessFail;
            @end
            @interface JsonRest : NSObject
            {
                id <JsonDataDelegate> delegate;
                NSURLConnection *theConnection;
                NSMutableData *webData;
                BOOL HadGotResponse;
                BOOL HasFailedConnection;
                UIAlertView *alert;
            }
            @property (retain) id delegate;
            @property(nonatomic,retain) NSURLConnection *theConnection;
            @property (nonatomic)BOOL HadGotResponse;
            @property (nonatomic)BOOL HasFailedConnection;
            -(void)GetServiceCall:(NSString *)url Paraqmeter:(NSString*)parameter;
            -(void)Set2Defaults;

            @end

            #import "JsonRest.h"
            #define BASE_URL @"http://www.abc.com"
            #define DOMAIN @"GETLIST"

            @implementation JsonRest
            @synthesize delegate,HadGotResponse,HasFailedConnection,theConnection;
            - (id)init
            {
                if (self) 
                {
                    // Initialization code.
                    //appdel = (ReachCastAppDelegate*)[[UIApplication sharedApplication]delegate];
                }
                return self;
            }

            -(void)GetServiceCall:(NSString *)url Paraqmeter:(NSString*)parameter
            {
                [SVProgressHUD startActivity : @"Loading..."];
                NSString *strUrl=[NSString stringWithFormat:@"%@%@?%@",BASE_URL,url,parameter];
                NSURL *url1=[NSURL URLWithString:strUrl];
                NSLog(@"domainURL :: %@",strUrl);
                NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url1];
                [req setTimeoutInterval:60.0];
                [req setHTTPMethod:@"GET"];
                //    if([HttpMethod isEqualToString:@"GET"])
                //    {
                //        [req setHTTPMethod:HttpMethod];
                //        
                //    }
                //    else if([HttpMethod isEqualToString:@"PUT"])
                //    {
                //        NSData *requestData = [NSData dataWithBytes: [BodyString UTF8String] length: [BodyString length]];
                //        
                //        [req setValue:valueforput forHTTPHeaderField:@"Content-type"];
                //        [req setHTTPMethod:HttpMethod];
                //        [req setHTTPBody: requestData];
                //        
                //    }
                //    else if ([HttpMethod isEqualToString:@"POST"]){
                //        
                //        NSData *requestData = [NSData dataWithBytes: [BodyString UTF8String] length: [BodyString length]];
                //        
                //        [req setValue:valueforput forHTTPHeaderField:@"Content-type"];
                //        [req setHTTPMethod:HttpMethod];
                //        [req setHTTPBody: requestData];
                //        
                //    }
                if(theConnection)
                    [self Set2Defaults];
                theConnection=[[NSURLConnection alloc]initWithRequest:req delegate:self];
                if(theConnection)
                    webData=[[NSMutableData data]retain];
                else
                    NSLog(@"Connection Failed !!!");

                NSLog(@"Has got response");
            }
            -(void)Set2Defaults {

                if (webData != nil){
                    [webData release];
                    webData = nil;
                }
                if (theConnection != nil) {
                    [theConnection release];
                    if (theConnection != nil) 
                        theConnection = nil;
                }
            }

            #pragma mark -
            #pragma mark NSURL Connection Delegate methods
            -(void) connection:(NSURLConnection *) connection didReceiveResponse:(NSURLResponse *) respons{
                [webData setLength: 0];

                NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)respons;
                int statusCode = [httpResponse statusCode];
                NSLog(@"statusCode ---- >> %d",statusCode);
                if (200 != statusCode) {    
                    //    if (401 == statusCode) {
                }
            }

            -(void) connection:(NSURLConnection *) connection didReceiveData:(NSData *) data {
                [webData appendData:data];
            }

            -(void) connection:(NSURLConnection *) connection didFailWithError:(NSError *) error {

                [SVProgressHUD stopActivity];
                [self Set2Defaults];
                self.HadGotResponse=NO;
                self.HasFailedConnection=YES;

                //    txtWindSpeed.text = @"";
                [[self delegate] JsonprocessFail];
                NSLog(@"Connection Failed!!!");

                //[self HideLoadingDialogue];

                alert = [[[UIAlertView alloc] initWithTitle:@"ERROR" message:@"Internet Connection Failed.Please try again." delegate:self cancelButtonTitle:nil otherButtonTitles:nil] autorelease];
                //alert.frame=CGRectMake(alert.frame.origin.x, alert.frame.origin.y, alert.frame.size.width, 130);
                NSLog(@"%F",alert.frame.origin.y);
                [alert show];
                alert.frame=CGRectMake(alert.frame.origin.x, alert.frame.origin.y+25, alert.frame.size.width, 130);

                //[noConnect release];

                [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(timerCall) userInfo:nil repeats:NO];
            }

            -(void)timerCall
            {
                [alert dismissWithClickedButtonIndex:0 animated:TRUE];
            }
            -(void)connectionDidFinishLoading:(NSURLConnection *)connection
            {
                [SVProgressHUD stopActivity];
                self.HasFailedConnection=NO;
                NSString *theXML;
                theXML = [[NSString alloc] initWithBytes: [webData mutableBytes] length:[webData length] encoding:NSASCIIStringEncoding];
                //NSLog(@"theXML Values : %@", theXML);

                //self.ResponseDict = [theXML JSONValue];

                [[self delegate] JsonprocessSuccessful:theXML];
                [theXML release];
            }

            -(void)dealloc
            {
                if (webData != nil){
                    [webData release];
                    webData = nil;
                }

                if (theConnection != nil) {
                    [theConnection release];
                    if (theConnection != nil) 
                        theConnection = nil;
                }
                [super dealloc];
               // [ResponseDict release];

            }
            @end

Sie können dies wie folgt verwenden

       -(void)getJsonCalling
            {
            @try {
                NSString *strParameter=[NSString stringWithFormat:@"param1=%@&param2=%@",param1,param2];
                objJsonRest=[[JsonRest alloc]init];
                [objJsonRest setDelegate:self];
                [objJsonRest GetServiceCall:DOMAIN Paraqmeter:strParameter];
                [objJsonRest release];
            }
            @catch (NSException *exception) {
                NSLog(@"Exception in %s %@",__FUNCTION__,exception);

            }
            }
            - (void) JsonprocessSuccessful: (NSString *)receivedData
            {
              }
            -(void) JsonprocessFail;
            {
                   NSLog(@"FAIL");
            }

CodeJaeger.com

CodeJaeger ist eine Gemeinschaft für Programmierer, die täglich Hilfe erhalten..
Wir haben viele Inhalte, und Sie können auch Ihre eigenen Fragen stellen oder die Fragen anderer Leute lösen.

Powered by:

X