0

我只是使用 xcode 3.2 的 ios 初学者,我想从 url 获取数据,我正在获取这样的 JSON 格式数据,如何显示 Windows Live ID,Google with Image,loginUrl,logoutUr ..

JSON

0
Name : "Windows Live™ ID"

LoginUrl : ""

LogoutUrl : ""

ImageUrl : ""

EmailAddressSuffixes

1

Name : "Google"

LoginUrl : ""

LogoutUrl : ""

ImageUrl : ""

EmailAddressSuffixes

2

Name : "Yahoo!"

LoginUrl : ""

LogoutUrl : ""

ImageUrl : ""

EmailAddressSuffixes

我必须在 .h 文件和 .m 文件中写什么,有人可以帮我吗?

提前致谢。

4

3 回答 3

0

一次尝试这样,在tableviewcellForRowAtIndex方法中使用这个

如果您想获取字典,请使用,

NSMutableDictionary *dict=[jsonArray objectAtIndex:indexPath.row];

如果您想获取字符串值,请使用此值,

NSString * LoginUrl=[[jsonArray objectAtIndex:indexPath.row]valueForKey:@"LoginUrl"];

如果您想获取所有数据,请使用一个 for 循环并保存该数据。

于 2013-04-02T07:20:24.293 回答
0

您可以使用 NSURLConnection(或)Json 库来解析从服务器接收到的数据。

下面是使用 NSURL 连接解析 JSON 的示例代码:

在 viewDidLoad 中:

NSString *strConfirmChallenge = [URL string];

     strConfirmChallenge = [strConfirmChallenge stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSLog(@"strConfirmChallenge=%@",strConfirmChallenge);

    NSURL *myURL = [NSURL URLWithString:strConfirmChallenge];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:myURL
                                                           cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
                                                       timeoutInterval:60];

    NSURLConnection *theConnection =[[NSURLConnection alloc] initWithRequest:request delegate:self];


    //Delegate methods

    - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
        responseData = [[NSMutableData alloc] init];
    }

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

    - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
        //NSLog(@"Failed to get data");
        self.view.userInteractionEnabled = TRUE;
        UIAlertView *myalert = [[UIAlertView alloc]initWithTitle:@"Info" message:@"Service error" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
        [myalert show];
    }

    - (void)connectionDidFinishLoading:(NSURLConnection *)connection
    {
        dispatch_async(kBgQue, ^{
            [self performSelectorOnMainThread:@selector(parseData:)
                                   withObject:responseData
                                waitUntilDone:YES];
        });
        self.view.userInteractionEnabled = TRUE;
    }



    //parsing data


    -(void)parseData:(NSData *)data{
         NSError *error;
            NSDictionary *data = [NSJSONSerialization JSONObjectWithData:data options:(0) error:&error];

            if(error){
                //NSLog(@"JSon Error %@",[error localizedDescription]);
                UIAlertView *myalert = [[UIAlertView alloc]initWithTitle:@"Info" message:@"Service error" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
                [myalert show];

            }else{
                NSDictionary *datDetails = (NSDictionary *) [data objectForKey:@"your key"];
        }
    }
于 2013-04-02T07:20:31.863 回答
0

您可以使用SBJson解析 json。在您的项目中添加 SBJson 类,然后使用此代码解析 json

-(void)WBCalled
{

    ExampleAppDataObject* theDataObject = [self theAppDataObject];

    //this code is used to send and retrive data from webservices
    NSString *post =@"";

    //NSLog(@"post is:%@",post);

    NSURL *url=[NSURL URLWithString:@""];

    NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];

    NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
    [request setURL:url];
    [request setHTTPMethod:@"POST"];
    [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
    [request setHTTPBody:postData];

    NSError *error = [[NSError alloc] init];
    NSHTTPURLResponse *response = nil;
    NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

    if ([response statusCode] >=200 && [response statusCode] <300)
    {
        NSString *responseData = [[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
        // NSLog(@"Response ==> %@", responseData);

        SBJsonParser *jsonParser = [SBJsonParser new];   //here you are using sbjson library to parse json data
        NSDictionary *jsonData = (NSDictionary *) [jsonParser objectWithString:responseData error:nil];
        NSLog(@"full jsonData : %@",jsonData);   //this dictionary contain all the data of your json response you can conver this data in NSString or NSArray according to your need

        NSString *message = (NSString *) [jsonData objectForKey:@"Message"];

        if (message.length !=0) {
            [self alertStatus:message :@"Status"];
        }


    } else {
        if (error) NSLog(@"Error: %@", error);
        [self alertStatus:@"Connection Failed" :@"Data Sending Failed!"];
    }

}
-(void) alertStatus:(NSString *)msg :(NSString *)title
{
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:title
                                                        message:msg
                                                       delegate:self
                                              cancelButtonTitle:@"Ok"
                                              otherButtonTitles:nil, nil];

    [alertView show];
}
于 2013-04-02T07:21:51.777 回答