如何从json响应和相应的值获取值传递相应的索引ios

时间:2015-08-10 12:20:41

标签: ios arrays json

我是ios的新手。我得到json的响应...我希望通过传递我从选择器视图中选择的所选专业人员的ID将数据传递给json。我不知道如何通过他们选择的专业人士传递选定的身份证。意味着当我发送专业人员时...我也得到了他们所选择的专业人员的身份。任何人都可以帮助我解决这个问题。

pro =     (
                {
            id = 2;
            pro = abcd;
        },
                {
            id = 3;
            pro = ab;
        },
              {
            id = 45;
            pro = Mango;
        }
    );
    status = 1;

//获取dictinary数据

if (responseData != nil)
    {

        jsonDict = (NSMutableDictionary *)[NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error];

        NSMutableDictionary *proDict = [jsonDict valueForKey:@"pro"];

       NSLog(@"MY DATA $$$$ %@",proDict);

        NSMutableDictionary *proDict2 = [proDict valueForKey:@"pro"];
        NSLog(@"My second Pro %%%% %@",proDict2);

        proArray = [proDict2 valueForKey:@"pro"];

        idOfPro = [proDict2 valueForKey:@"id"];

 //   
        idOfPro = [jsonDict objectForKey:@"pro"];



        NSLog(@"My pro array %@",idOfPro[0]);

        NSMutableDictionary *itemDict = [[NSMutableDictionary alloc]init];
        for (NSDictionary *proItemDict in idOfPro)
        {
            [itemDict setObject:proItemDict forKey:proItemDict[@"pro"]];
            NSLog(@"My dictionary of selected index value %@",proItemDict);

        }

      }
// Response data i get 
caseStatus =     {
        Claimcase = 25;
        completeBad = 0;
        completecase = 0;
        feedbackAwating = 0;
        openDelcase = 4;
        status = 1;
    };
    pro =     {
        pro =         (
                        {
                id = 2;
                pro = abcd;
            },
                        {
                id = 3;
                pro = ab;
            },//// so on......
   }
        );
        status = 1;
    };
    reminder =     {
        msg = "No reminder for today";
        status = 0;
    };
    type = dashboard;
}

1 个答案:

答案 0 :(得分:1)

它非常简单,有不同的方法可以做到。我选择简单的方法并按如下方式分享逻辑。 1. json结果有两个值,即pro和status。 2.Pro是一系列字典。 即:`

NSArray *proArr=[jsonResDict objectForKey:@"pro"];
proArr[0]=   {
            id = 2;
            pro = abcd;
        }
proArr[1]={
            id = 3;
            pro = ab;
        }
`

它继续......

3.要获得pro值并且其id是关键部分,您只需要使用proArr迭代循环。

 NSMutableDictionary *itemDict=[[NSMutableDictionary alloc]init];  
 for(NSDictionary *proItemDict in proArr)
     {
       [itemDict setObject:proItemDict forKey:proItemDict[@"pro"]];
//storing the entire dictionary with item name and id for the name key.
//name is what you will be getting from picker.And you can easily get the id by taking the relative item from dictionary whose key matches to the name
    }