有一部分我的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 ORGU00c2NICOS";
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 的值和其中的其他信息来执行与之相关的按钮,使用这个//http://api.iospond.com/api/index.php/GetData 我没有任何问题,但是如果我使用它//http://blessing.com.br/aplicativos/pontos.php 我会收到此错误 线程 1:信号 SIGABRT, 有人可以帮忙吗?
你应该使用 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);
}
}];
- 然后检查您获取数组或字典以逐步解析。
NSDictionary *dic in json
只会给你钥匙。要获取其值,请使用
json[dic]