当我尝试使用我的 Json 时,它只显示第一个值(标题)Objective c

Posted

技术标签:

【中文标题】当我尝试使用我的 Json 时,它只显示第一个值(标题)Objective c【英文标题】:When I try to use my Json it shows only the first value (title) Objective c 【发布时间】:2017-05-11 13:39:04 【问题描述】:

有一部分是我的Json,


    pontos =     (
                
            Id = 1;
            Latitude = "-22.739";
            Longitude = "-47.335";
            cidade = Americana;
            endereco = "RUA WASHINGTON LUIZ, 742, CENTRO, Americana/SP";
            letra = "letter-A";
            localizacao = "https://www.google.com.br/maps/dir//Quinoa+Natural+-+Washington+Luiz,+742+-+Centro,+Americana+-+SP,+13465-520/@-22.7399455,-47.3349734,19.5z/data=!4m15!1m6!3m5!1s0x0:0xa4eca82f1081bbd9!2sQuinoa+Natural!8m2!3d-22.739908!4d-47.3353712!4m7!1m0!1m5!1m1!1s0x94";
            nome = "QUINOA NATURAL";
            site = "WWW.QUINOANATURAL.COM.BR";
            telefone = "(19) 3405-6006";
        ,
                
            Id = 2;
            Latitude = "-22.752";
            Longitude = "-47.345";
            cidade = Americana;
            endereco = "AV. BRASIL, 1740, VILA FREZZARIN, Americana/SP";
            letra = "letter-A";
            localizacao = "https://www.google.com.br/maps/dir//Av.+Brasil+Norte,+1740+-+Vila+Frezzarin,+Americana+-+SP/@-22.7522934,-47.3479628,17z/data=!4m13!1m4!3m3!1s0x94c89bd929bbb37d:0x97700c95cf048c5d!2sAv.+Brasil+Norte,+1740+-+Vila+Frezzarin,+Americana+-+SP!3b1!4m7!1m0!1m5!1";
            nome = "TERRANUTRA ORG\U00c2NICOS";
            site = "";
            telefone = "(19) 3408-0032";
        ,

这是我的代码

- (void)viewDidLoad

    int test = 40;
    UIScrollView *sView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 350, 460)];
    [self.view addSubview:sView];
    //http://blessing.com.br/aplicativos/pontos.php
    //http://api.iospond.com/api/index.php/GetData
    //setting the json as data
    NSData *data=[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://blessing.com.br/aplicativos/pontos.php"]];
    NSError *error=nil;
    //Encoding the data to string
    NSString *teste = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

    //show the code json

    //putting the json in an arraylist
    NSArray *json = [NSJSONSerialization JSONObjectWithData:[teste dataUsingEncoding:NSUTF8StringEncoding] options:kNilOptions error:&error ];
    CGFloat xPos = 0;
    CGFloat x = 0;
    CGFloat y = 10;
    CGFloat yPos = 10;
    for (NSDictionary *dic in json) 
        //show Ids catched in the json
        NSLog(@"%@", dic);
        //show names catched uin the json

        //set the ids of json  as number of button.tag
        int numberOfButtons = [dic[@"id"] integerValue];
        // show number of button.tag


        if(xPos == 170)

            NSMutableArray *mutableArray = [[NSMutableArray alloc] init];
            yPos +=20;
            //create a button
            self.button = [UIButton alloc];
            self.button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
            self.button.frame = CGRectMake(xPos, yPos, 40, 20);
            button.tag = numberOfButtons;
            //set the button title for the normal state
            [self.button setTitle:[dic objectForKey:@"id"] forState:UIControlStateNormal];
            //set the button title for when the finger is pressing it down
            [self.button setTitle:[dic objectForKey:@"name"] forState:UIControlStateHighlighted];
            //add action to capture the button press down event
            [self.button addTarget:self action:@selector(buttonIsPressed:)forControlEvents:UIControlEventTouchDown];
            //add the button to the view
            [sView addSubview:self.button];

            //Button Styles
            yPos += 10;
            test = 50 + yPos;



            //Add the button into array
            [mutableArray addObject:button];
            //show code button


            xPos = 0;
            x = 10;

        


        if(x == 0)


    NSMutableArray *mutableArray = [[NSMutableArray alloc] init];


        //adding 10 to change the location of the next button that will be create

        y +=20;
        //create a button
        self.button = [UIButton alloc];
        self.button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        self.button.frame = CGRectMake(x, y, 40, 20);
        button.tag = numberOfButtons;
        //set the button title for the normal state
        [self.button setTitle:[dic objectForKey:@"id"] forState:UIControlStateNormal];
        //set the button title for when the finger is pressing it down
        [self.button setTitle:[dic objectForKey:@"name"] forState:UIControlStateHighlighted];
        //add action to capture the button press down event
        [self.button addTarget:self action:@selector(buttonIsPressed:)forControlEvents:UIControlEventTouchDown];
        //add the button to the view
        [sView addSubview:self.button];

        //Button Styles
        y += 10;





        //Add the button into array
        [mutableArray addObject:button];
        //show code button
       //NSLog(@"%@", mutableArray);

        xPos = 170;

    

        if (x == 10) 

            x = 0;

        
        CGSize sViewContentSize = CGSizeMake(320, test);
        [sView setContentSize:sViewContentSize];
      //NSLog(@"%i", test);
    


这是日志

2017-05-11 10:08:16.589 tentativaJSON[4082:90b] pontos

所以我想捕获pontos 的详细信息,但它只显示它,我需要使用 Id`s 的值和其中的其他信息来执行与其相关的按钮,使用这个 //http://api.iospond.com/api/index.php/GetData I没有任何问题,但如果我使用这个 //http://blessing.com.br/aplicativos/pontos.php 我得到这个错误线程 1:信号 SIGABRT,有人可以帮忙吗?

【问题讨论】:

您在 JSN 响应中获取字典。所以只需获取关键的“pontos”。从这里你会得到字典数组。记住 '' 代表 Dictionary 而 '[' 代表 Array 【参考方案1】:

你应该使用 GET :-

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:[NSString 
 stringWithFormat:@"http://api.iospond.com/api/index.php/GetData"]]];
[request setHTTPMethod:@"GET"];
[request setTimeoutInterval:60];

[NSURLConnection sendAsynchronousRequest:request queue:
[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, 
NSData *data, NSError *error) NSString *str = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];

if ([(NSHTTPURLResponse *)response 
              statusCode] == 200) 

                               NSArray *dict = (NSArray *)str;

                               NSLog(@"Parse here:: %@",dict);

                           else 
                               NSLog(@"elseelse 
          %@",error.localizedDescription);
                           
                       ];
然后检查您获取数组或字典以进行逐步解析..

【讨论】:

NSURLConnection 在 iOS9 中已弃用 所以使用这个 URL api.iospond.com/api/index.php/GetData 我没有任何问题可以使用我的代码,并且使用你展示的这种方式,我会继续做我用来创建按钮的 id json?【参考方案2】:
NSDictionary *dic in json

只会给你钥匙。 要获取它们的值,请使用

json[dic]

【讨论】:

所以我会这样放? for (NSDictionary *dic in json[dic]) 您可以执行以下操作:for (NSDictionary *dic in json) value = json[dic]。然后使用价值 那不行,它显示一个错误,它的值是它未声明的标识符 value 是一个新的标识符——你应该在定义它时使用一个类型,例如NSDictionary *value = json[dic]。从此使用 value 代替 dic 如:int numberOfButtons = [value[@"id"] integerValue]; 当我将其定义为 NSDictionary *value = json[dic]; 时也显示错误,`在 'NSArray *' 类型的对象上找不到读取字典元素的预期方法`

以上是关于当我尝试使用我的 Json 时,它只显示第一个值(标题)Objective c的主要内容,如果未能解决你的问题,请参考以下文章

当我使用 ngrok 加载我的 Laravel 应用程序时,它只显示基本的 html 而没有 css

如何展平多级/嵌套 JSON?

试图在我的存档页面中显示所有帖子,但它只返回页面内容

当我尝试从网页添加 DOM 元素时,我的数组得到 0 值,如何将值添加到 .each 函数中的列表?

当我访问我的 iframe 时,当我使用 selenium 时,#document 不显示

Swift 和 Alamofire - 访问条件语句的 JSON 值