Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何使用nsserlization解析nsdictionary数据。?_Ios_Objective C_Serialization_Nsdictionary - Fatal编程技术网

Ios 如何使用nsserlization解析nsdictionary数据。?

Ios 如何使用nsserlization解析nsdictionary数据。?,ios,objective-c,serialization,nsdictionary,Ios,Objective C,Serialization,Nsdictionary,我有这样的字典数据和图像上的一个数组 {结果:成功,数据:{id:12,产品名称:12\Round 板材,sku:ECOW12RP,描述:直径12英寸x\t深度0.9 英寸,价格:153.00,商业价格:365.00,图片:[{image:1454499068ecow12rp_01.jpg}],包装尺寸:20,商业包装尺寸:50,类别:2,3,税务类别:1,创建日期:2016-02-03,更改日期:2016-02-03 17:52:58,状态:1,已删除:0,安排:1,传递:150.00} 我想

我有这样的字典数据和图像上的一个数组

{结果:成功,数据:{id:12,产品名称:12\Round 板材,sku:ECOW12RP,描述:直径12英寸x\t深度0.9 英寸,价格:153.00,商业价格:365.00,图片:[{image:1454499068ecow12rp_01.jpg}],包装尺寸:20,商业包装尺寸:50,类别:2,3,税务类别:1,创建日期:2016-02-03,更改日期: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中的字符串,但我得到了ProductDetails数组null,我不知道为什么它得不到键值

我正在解析nsdictionary中的数据,但当我尝试解析此json数据中的图像数组时,我有空数组。请查看此json数据


关于将产品名称数据输入词典的具体问题,这将起作用

   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


在很多方面,这可能会失败。只有你可以通过调试告诉我们在哪里。你在这里得到值吗-NSLog@arr%@,字典?ok@trojanfoe如果我的代码现在没有崩溃,但是当我尝试在数组中添加临时值,然后打印arrayproductdetail时,它给了我空值。有人能帮我吗?请在productdetail数组中添加临时值之前添加断点。通过查看调试区域,确保您的productdetail数组不能为零。此行为get me nullvalueNSLog@theProductName:%@[temp objectForKey:@product\u name]的临时字典值;下面是ProductName:nullshow的临时字典值,一旦你在这里得到了什么结果,字典就是一些Anbu.Karthik先生,我可以对你说些什么。你总是在摇Sir这是我得到的输出,这就是我想要的2016-02-04 16:33:31.934 Ecoware[1356:96515]产品详细信息{product_name=12\Round Plate;}我现在需要修改我儿子的基础知识,代码需要更多的错误检查。如果产品名称在输入数据中不存在,它将崩溃。
NSLog(@"the temp dictionary value for ProductName: %@", [temp objectForKey:@"product_name"];
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];
 }