Ios 使用JSON的对象和键的UIPicker
我有点被UIpicker困住了。以下是我的JSON数据:Ios 使用JSON的对象和键的UIPicker,ios,objective-c,json,uipickerview,Ios,Objective C,Json,Uipickerview,我有点被UIpicker困住了。以下是我的JSON数据: { "contact_id": [ "455", "464" ], "contact_name": [ "Administrator", "Main contact" ] } 我将数据存储到NSdictionary中,然后在转换到对象后,我创建了一个NSarray来存储联系人。在我的Viewcontroller.m中,我有: 使用JSON的对
{
"contact_id": [
"455",
"464"
],
"contact_name": [
"Administrator",
"Main contact"
]
}
我将数据存储到NSdictionary中,然后在转换到对象后,我创建了一个NSarray来存储联系人。在我的Viewcontroller.m中,我有:
使用JSON的对象和键的UIPicker
// Creating an URL object
NSURL *url = [NSURL URLWithString:@"http://web.com/json.php"];
//Creating the data object that will hold the content of the URL
NSData *jsonData = [NSData dataWithContentsOfURL:url];
NSError *error = nil;
NSDictionary *result = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error];
contactlist = [result objectForKey:@"contact_name"];
我能够使用contactlist向下拉列表显示联系人值。这是我的其他代表
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
return 1;
}
// returns the # of rows in each component..
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
return [contactlist count];
}
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
return [contactlist objectAtIndex:row];
}
- (void)pickerView:(UIPickerView *)pickerView
didSelectRow:(NSInteger)row
inComponent:(NSInteger)component{
contactTextField.text = (NSString *)[contactlist objectAtIndex:row];
}
我想发送一个JSON post请求,其中包含所选联系人的相应id
例如,如果您选择contact_nameAdministrator
,我想获取其联系人id455
我创建了一个方法
- (IBAction)SendMessage:(id)sender ;
这将在将JSON数据发送到服务器端脚本之前创建JSON数据,这是我需要传递id而不是联系人姓名的地方
// NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:@"603", @"contact",nil];
我创建此阵列是为了解决问题,但无法使其工作
contactid = [result objectForKey:@"contact_id"];
好吧,我想我知道你的问题。可能有几种解决方案。但我会通过一个刚进入我体内的 如果id的数组与名称的顺序相同,只需创建一个新数组
NSDictionary *result = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error];
NSArray* ids = (NSArray*)[result objectForKey:@"contact_id"];
然后在方法中:
- (void)pickerView:(UIPickerView *)pickerView
didSelectRow:(NSInteger)row
inComponent:(NSInteger)component{}
执行以下操作以获取相应的id:
NSString *idSentToServer = (NSString *)[ids objectAtIndex:row];
我确实理解JSON在您的案例中的定义方式可能是这样的。 JSON数据是否应该返回JSON对象数组 例如:
[{"contact_id":"455","contact_name":"Administrator"},{"contact_id":"464","contact_name":"Main contact"}]
我不清楚你在这里想要实现什么?“我想在我的JSON post请求中发送所选联系人的相应id”这句话是什么意思?您可能有一个想要实现JSON正文示例的示例?我刚刚编辑了我的post,我还不清楚您是否无法正确格式化JSON或从方法-(void)获取联系人idpickerView:(UIPickerView*)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)组件{contactTextField.text=(NSString*)[联系人列表对象索引:行]?很清楚:我得到JSON及其有效值。我可以用contact_name值填充选择器。现在我需要得到contact_ID,因为这是我的服务器关心的全部。你是问我问题吗?还是建议我重新格式化我的JSON?不管怎样,你的JSON是有效的,它向我解释一个包含ID集合和集合的对象n个名字。我也愿意接受建议,你认为你的格式更容易使用吗?对不起,我很忙,不在。是的,我认为如果json被格式化为带有两个对象的json数组(在我的评论中提供),那么你发布的原始代码可能会起作用