Good day. I need help with JSON on iOS. I need to decipher the numbers in the "des" section and put it in the Label
. All parsers are too large and complex, there is no simple lesson. = (Help ...
This is all the code of the page to be parsed.
{"des":1.00,"dem":1085.16,"spo":1.44,"def":0.66,"cap":1.84,"all":11739,"win":53.42,"lvl":6.77,"status":"noerror","name":"_shuzik_"}
PS Don't kick me hard, I'm new. Honestly, I tried to figure it out = (
If someone needs to use, I found the easiest (For me) option
We add AFNetworking
and HTMLParser
to the project, you can download it everywhere.
In .h
@property (nonatomic, strong) IBOutlet UILabel *allLabel;
I added by need UILabel
In .m
#import "InfoViewController.h" #import "HTMLParser.h" #import "AFJSONRequestOperation.h" // тут все теги которые нам нужны, можно писать отдельно, можно через заятую в зависимости от того что вам нужно. #define iAll @"all" @interface InfoViewController () @end @implementation InfoViewController @synthesize allLabel = _allLabel; - (void)viewDidLoad { [super viewDidLoad]; NSURL *url = [NSURL URLWithString:@"http://......."]; NSURLRequest *request = [NSURLRequest requestWithURL:url]; [AFJSONRequestOperation addAcceptableContentTypes:[NSSet setWithObject:@"text/html"]]; AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) { NSLog(@"JSON %@", JSON); NSDictionary *data = (NSDictionary *)JSON; if (data) { // так как на выходе получаем нс намбер, его нужно завернуть в нс стринг - иначе краш NSString *editAlltext = [NSString stringWithFormat:@"%@", [data objectForKey:iAll]]; self.allLabel.text = editAlltext; } } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON){ NSLog (@"Err : %@", error); }]; [operation start]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
I work through the XIB, so after that, I went to the XIB and threw a Label on the monitor and connected it to allLabel
I hope I wrote it clearly. Use!