我是ios新手。我正在从json获得响应,例如...我想通过传递我从选择器视图中选择的选定职业的ID将数据传递给json。我不知道如何通过他们选择的专业人士传递选择的ID。意味着当我发送专业人士时..我还获得了所选专业人士的ID。任何人都可以帮我解决这个问题。

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


//获取判别数据

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. json结果有两个值,即pro和status。
2.Pro是一组字典。
即:`

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


然后继续

3.要获取专业值及其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
    }

10-06 07:43
查看更多