[英]How to parse nsdictionary data using nsserlization.?
我有这样的字典数据和图像上的一个数组。
{“结果”:“成功”,“数据”:{“ id”:“ 12”,“产品名称”:“ 12 \\”圆盘“,” sku“:” ECOW12RP“,”描述“:”直径12英寸x \\ tDepth 0.9 inch“,” price“:” 153.00“,” business_price“:” 365.00“,” image“:[{” image“:” 1454499068ecow12rp_01.jpg“}],” pack_size“:” 20“,” business_pack_size“:” 50“,” category“:” 2,3“,” tax_class“:” 1“,” created“:” 2016-02-03“,” altered“:” 2016-02-03 17:52 :58“,”状态“:” 1“,”已删除“:” 0“,”安排“:” 1“,”交付“:” 150.00“}}
我想从中解析所有关键值。 这是我用于此任务的代码。
-(void)viewDidLoad
{
NSLog(@"d %ld", (long)id);
NSString* myNewString = [NSString stringWithFormat:@"%i", id];
NSURL *producturl = [NSURL URLWithString:@"http://dev1.brainpulse.org/ecoware1/webservices/product/" ];
NSURL *url = [NSURL URLWithString:myNewString relativeToURL:producturl];
NSData * imageData = [NSData dataWithContentsOfURL:url];
UIImage * productimage = [UIImage imageWithData:imageData];
NSURL *absURL = [url absoluteURL];
NSLog(@"absURL = %@", absURL);
NSURLRequest *request= [NSURLRequest requestWithURL:absURL];
[NSURLConnection connectionWithRequest:request delegate:self];
}
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(nonnull NSURLResponse *)response
{
data = [[NSMutableData alloc] init];
NSLog(@"Did receive response");
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)thedata
{
[data appendData:thedata];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
NSDictionary *dictionary = [[NSJSONSerialization JSONObjectWithData:data options:0 error:nil]objectForKey:@"data"];
NSLog(@"arr %@", dictionary);
[productdetail removeAllObjects];
for (NSString *tmp in dictionary)
NSMutableDictionary *temp = [NSMutableDictionary new];
[temp setObject:@"product_name" forKey:@"product_name"];
//[temp setObject:[tmp objectForKey:@"id"] forKey:@"id"];
// [temp setObject:[tmp objectForKey:@"image"] forKey:@"image"];
[productdetail addObject:temp];
NSLog(@"detail %@", productdetail);
}
我试图在for循环的帮助下从nsdictionary
解析字符串,但是我将产品详细信息数组设置为null,但我不知道为什么它没有获得键值。
我正在解析nsdictionary中的数据,但是当我尝试解析此json数据中的图像数组时,我具有null数组,请查看此json数据。
{"result":"Successful","data":{"id":"2","product_name":"6\" Round Plate","sku":"ECOW6RP","description":"Diameter 6.0 (inch) x Depth 0.6 (inch)\r\n\r\nPerfect for finger foods!","price":"42.89","business_price":"100.00","image":[{"image":"1454499251ecow6rp_01.jpg"}],"pack_size":"20","business_pack_size":"50","category":"2,3","tax_class":"1","created":"2016-01-19","altered":"2016-02-06 16:06:10","status":"1","deleted":"0","arrange":"1","delivery":"150.00"}}
关于将product_name
数据放入字典的具体问题,这可以解决
NSDictionary *dictionary = [[NSJSONSerialization JSONObjectWithData:data options:0 error:nil]objectForKey:@"data"];
NSMutableDictionary *temp = [NSMutableDictionary new];
if ([dictionary objectForKey:@"product_name"]){
[temp setObject:[dictionary objectForKey:@"product_name"] forKey:@"product_name"];
}
如果您打印出制作的字典,您应该会看到它在其中。
NSLog(@"the temp dictionary value for ProductName: %@", [temp objectForKey:@"product_name"];
尝试这个
选项1
NSDictionary *dictionary = [[NSJSONSerialization JSONObjectWithData:data options:0 error:nil]objectForKey:@"data"];
[productdetail removeAllObjects];
if (dictionary)
{
NSMutableDictionary *temp = [NSMutableDictionary new];
[temp setObject:[dictionary objectForKey:@"product_name"] forKey:@"product_name"];
[productdetail addObject:temp];
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.