gpt4 book ai didi

ios - Json解析后取回数据

转载 作者:行者123 更新时间:2023-11-29 13:00:25 33 4
gpt4 key购买 nike

在我的 iOS 应用程序中,我必须解析一个 JSON 文件。从这个 JSON 我需要以下内容:名称、图像宽度和图像高度。要获取图像名称我没有任何问题,要获取图像的高度和高度我使用以下代码:

- (void) loadImageFromWeb:(NSString *)urlImg forName:(NSString*)name {
NSURL* url = [NSURL URLWithString:urlImg];
//NSURLRequest* request = [NSURLRequest requestWithURL:url];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];

NSString *authCredentials =@"reply:reply";
NSString *authValue = [NSString stringWithFormat:@"Basic %@",[authCredentials base64EncodedStringWithWrapWidth:0]];
[request setValue:authValue forHTTPHeaderField:@"Authorization"];

[NSURLConnection sendAsynchronousRequest:request
queue:[NSOperationQueue mainQueue]
completionHandler:^(NSURLResponse * response,
NSData * data,
NSError * error) {
if (!error){
UIImage* image = [[UIImage alloc] initWithData:data];
imageWidth = image.size.width;
imageHeight = image.size.height;
imgWidth = [NSString stringWithFormat:@"%f", imageWidth];
imgHeight = [NSString stringWithFormat:@"%f", imageHeight];
self.dictWithDataForPSCollectionView = @{@"title": name,
@"width": imgWidth,
@"height": imgHeight};
[self.arrayWithData addObject:self.dictWithDataForPSCollectionView];
NSLog(@"DATA ARRAY: %@", self.arrayWithData);
} else {
NSLog(@"ERRORE: %@", error);
}

}];
}

您可以看到我将名称、图像宽度和图像高度保存在 NSDictionary 中,然后将其放入 NSMutableArray 中。当它执行 NSLog 时,我看到了这个:

DATA ARRAY: (
{
height = "512.000000";
title = "Eau de Toilet";
width = "320.000000";
},
{
height = "1049.000000";
title = "Eau de Toilet";
width = "1405.000000";
}
)

我的问题是如何在调用我的 json 解析器的类中获取此信息,我尝试以这种方式访问​​变量:

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

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[recivedData appendData:data];
NSString *string = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"JSON: %@", string);
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSDictionary *json;
NSError *err;

json = [NSJSONSerialization JSONObjectWithData:recivedData options:NSJSONReadingMutableLeaves error:&err];
JsonCategoryReader *reader = [[JsonCategoryReader alloc]init];
[reader parseJson:json];
}

但是当我运行代码时,它显示了一个空数组。我怎样才能得到这门课的信息?

更新:我要解析的 JSON 如下:

{
"1":{
"entity_id":"1",
"type_id":"simple",
"sku":"EAU_DE_TOILET_1",
"description":"A passionate scent with the zest of exclusive Zegna Bergamot, sparked by Violettyne Captive, and the warmth of Vetiver and Cedarwood",
"short_description":"EAU DE TOILETTE NATURAL SPRAY",
"meta_keyword":null,
"name":"Eau de Toilet",
"meta_title":null,
"meta_description":null,
"regular_price_with_tax":60,
"regular_price_without_tax":60,
"final_price_with_tax":60,
"final_price_without_tax":60,
"is_saleable":true,
"image_url":"http:\/\/54.204.6.246\/magento8\/media\/catalog\/product\/cache\/0\/image\/9df78eab33525d08d6e5fb8d27136e95\/p\/r\/product_100ml.png"
},
"2":{
"entity_id":"2",
"type_id":"simple",
"sku":"EAU_DE_TOILET_2",
"description":"A passionate scent with the zest of exclusive Zegna Bergamot, sparked by Violettyne Captive, and the warmth of Vetiver and Cedarwood",
"short_description":"EAU DE TOILETTE NATURAL SPRAY",
"meta_keyword":null,
"name":"Eau de Toilet",
"meta_title":null,
"meta_description":null,
"regular_price_with_tax":60,
"regular_price_without_tax":60,
"final_price_with_tax":60,
"final_price_without_tax":60,
"is_saleable":true,
"image_url":"http:\/\/54.204.6.246\/magento8\/media\/catalog\/product\/cache\/0\/image\/9df78eab33525d08d6e5fb8d27136e95\/s\/c\/scheda_non_shop.jpg"
}
}

我的方法 parseJson 执行以下操作:

- (void)parseJson:(NSDictionary *)jsonDict {

// Controllo che il json sia stato ricevuto
if (jsonDict) {
self.nameArray = [[NSMutableArray alloc]init];
self.imgUrlArray = [[NSMutableArray alloc]init];
self.dictWithDataForPSCollectionView = [[NSDictionary alloc]init];
self.arrayWithData = [[NSMutableArray alloc]init];
[self createArrayWithJson:jsonDict andIndex:1];
[self createArrayWithJson:jsonDict andIndex:2];
}

- (void)createArrayWithJson:(NSDictionary*)json andIndex:(NSString*)i {
NSDictionary *products = [json objectForKey:i];
NSString *name = [products objectForKey:@"name"];
NSString *imgUrl = [products objectForKey:@"image_url"];
// Scarico l'immagine e calcolo le dimensioni
if (name != nil && imgUrl != nil) {
[self loadImageFromWeb:imgUrl forName:name];
}
}

我希望你明白我做了什么

最佳答案

发生的事情是你的类是在你的 json 下载之前创建的,为了有一个好的序列,你必须调用你的方法来解析 completionHandler block 中的 json,当你确定它正在下载时。然后当你有你的对象加载时,你可以像这个例子一样解析它:

for (NSDictionary *dic in  reader.arrayWithData){

NSLog("height: %@",[dic objectForKey:@"height"]);
NSLog("title: %@",[dic objectForKey:@"title"]);
NSLog("width: %@",[dic objectForKey:@"width"]);

}

关于ios - Json解析后取回数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19903902/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com