[英]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.