如何将 Web 服务响应添加到目标 C、IOS 中的 NSDictionary 中



我正在 asp.net Web服务中获得响应表单,当我尝试将该NSData响应转换为NSDictionary格式时,它存储了nil。那么如何将其转换为NSDictionary呢?提前致谢

这是我的代码:

  -(void)jsonresponse:(NSMutableString *)json
   {
    NSData *data=[json dataUsingEncoding:NSUTF8StringEncoding];
NSError *error=nil;
NSDictionary *json1=[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];

tableArray= [NSMutableArray array];
   // menuArray=[NSMutableArray array];
for (NSDictionary *dic in [json1 objectForKey:@"Table"])
{
    [tableArray addObject:@{@"StudentId" : [dic objectForKey:@"StudentId"],
                         @"UserID" : [dic objectForKey:@"UserID"],
                         @"StudentName" : [dic objectForKey:@"StudentName"],
                         @"UserTypeName" : [dic objectForKey:@"UserTypeName"],
                         @"Path" : [dic objectForKey:@"Path"],
                         @"UnreadMessageCount" : [dic objectForKey:@"UnreadMessageCount"],
                         @"LastMessageTime" : [dic objectForKey:@"LastMessageTime"],
                         @"Image" : [dic objectForKey:@"Image"]}];
}
NSLog(@"table Array is==%@",tableArray);
for (NSDictionary *dic in [json1 objectForKey:@"Table1"])
{
    [menuArray addObject:@{@"MenuName":[dic objectForKey:@"MenuName"],
                           @"MenuName1" : [dic objectForKey:@"UserID"],
                           @"Icon" : [dic objectForKey:@"Icon"],
                           @"URL" : [dic objectForKey:@"URL"],
                           @"UserTypeConst" : [dic objectForKey:@"Path"]}];
}
 //   [self performSegueWithIdentifier:@"loginbutton" sender:nil];


NSLog(@"Menu Array is==%@",menuArray);
}

这是我的回应

{
"Table": [{
    "StudentId": "755",
    "UserID": "1618",
    "StudentName": "VRITEE SHAH",
    "UserTypeName": "Student",
    "Path": "",
    "UnreadMessageCount": "103",
    "LastMessageTime": "12/02/2016 16:52:30",
    "Image": ""
}, {
    "StudentId": "2629",
    "UserID": "5380",
    "StudentName": "NILESH PINGALE",
    "UserTypeName": "Student",
    "Path": "",
    "UnreadMessageCount": "68",
    "LastMessageTime": "12/05/2016 14:41:51",
    "Image": ""
}, {
    "StudentId": "2630",
    "UserID": "5386",
    "StudentName": "MEGAN ABRAHANM",
    "UserTypeName": "Student",
    "Path": "",
    "UnreadMessageCount": "190",
    "LastMessageTime": "12/07/2016 17:04:06",
    "Image": ""
}, {
    "StudentId": "2828",
    "UserID": "5784",
    "StudentName": "VAIBHAVI JOSHI",
    "UserTypeName": "Student",
    "Path": "",
    "UnreadMessageCount": "21",
    "LastMessageTime": "12/05/2016 14:41:51",
    "Image": ""
}, {
    "StudentId": "2866",
    "UserID": "5856",
    "StudentName": "ISHITA BACHANI",
    "UserTypeName": "Student",
    "Path": "",
    "UnreadMessageCount": "2",
    "LastMessageTime": "10/30/2016 10:52:48",
    "Image": ""
}],
"Table1": [{
    "MenuName": "Student Info",
    "MenuName1": "Profile ",
    "Icon": "http://192.168.0.125:85/Images/NavigationIcon/student_info.png ",
    "URL": "StudentProfile ",
    "UserTypeConst": "PAR"
}, {
    "MenuName": "Student Info",
    "MenuName1": "Calendar ",
    "Icon": "http://192.168.0.125:85/Images/NavigationIcon/calender.png ",
    "URL": "Calendar ",
    "UserTypeConst": "PAR"
}, {
    "MenuName": "Student Info",
    "MenuName1": "Time Table ",
    "Icon": "http://192.168.0.125:85/Images/NavigationIcon/timetable.png ",
    "URL": "TimeTable ",
    "UserTypeConst": "PAR"
}, {
    "MenuName": "Student Info",
    "MenuName1": "View Attendance ",
    "Icon": "http://192.168.0.125:85/Images/NavigationIcon/view_attendance.png ",
    "URL": "ViewAttendance ",
    "UserTypeConst": "PAR"
}, {
    "MenuName": "Transportation",
    "MenuName1": "Map View ",
    "Icon": "http://192.168.0.125:85/Images/NavigationIcon/map_view.png ",
    "URL": "MapView ",
    "UserTypeConst": "PAR"
}, {
    "MenuName": "Transportation",
    "MenuName1": "Alerts & Notification ",
    "Icon": "http://192.168.0.125:85/Images/NavigationIcon/alerts&notification.png ",
    "URL": "AlertsNotification ",
    "UserTypeConst": "PAR"
}, {
    "MenuName": "Transportation",
    "MenuName1": "Transportation Desk ",
    "Icon": "http://192.168.0.125:85/Images/NavigationIcon/transportation_desk.png ",
    "URL": "TransportationDesk ",
    "UserTypeConst": "PAR"
}, {
    "MenuName": "Notification/Alerts",
    "MenuName1": "Notification ",
    "Icon": "http://192.168.0.125:85/Images/NavigationIcon/notification.png ",
    "URL": "Notification ",
    "UserTypeConst": "PAR"
}, {
    "MenuName": "Notification/Alerts",
    "MenuName1": "Principal’s Desk ",
    "Icon": "http://192.168.0.125:85/Images/NavigationIcon/principal_desk.png ",
    "URL": "PrincipalDesk ",
    "UserTypeConst": "PAR"
}, {
    "MenuName": "Notification/Alerts",
    "MenuName1": "Digital Diary ",
    "Icon": "http://192.168.0.125:85/Images/NavigationIcon/digital_diary.png ",
    "URL": "DigitalDiary ",
    "UserTypeConst": "PAR"
}, {
    "MenuName": "Notification/Alerts",
    "MenuName1": "Assignment ",
    "Icon": "http://192.168.0.125:85/Images/NavigationIcon/assignment.png ",
    "URL": "Assignment ",
    "UserTypeConst": "PAR"
}, {
    "MenuName": "Payment Info",
    "MenuName1": "View Bill ",
    "Icon": "http://192.168.0.125:85/Images/NavigationIcon/view_bill.png ",
    "URL": "ViewBill ",
    "UserTypeConst": "PAR"
}, {
    "MenuName": "Payment Info",
    "MenuName1": "Pay Bill ",
    "Icon": "http://192.168.0.125:85/Images/NavigationIcon/pay_bill.png ",
    "URL": "PayBill ",
    "UserTypeConst": "PAR"
}, {
    "MenuName": "Payment Info",
    "MenuName1": "Online Payment History ",
    "Icon": "http://192.168.0.125:85/Images/NavigationIcon/onlinepayment_history.png ",
    "URL": "OnlinePaymentHistory ",
    "UserTypeConst": "PAR"
}, {
    "MenuName": "Change Password",
    "MenuName1": "Change Password ",
    "Icon": "http://192.168.0.125:85/Images/NavigationIcon/change_password.png ",
    "URL": "ChangePassword ",
    "UserTypeConst": "PAR"
}, {
    "MenuName": "Delete User",
    "MenuName1": "Delete User ",
    "Icon": "http://192.168.0.125:85/Images/NavigationIcon/delete.png ",
    "URL": "DeleteUser ",
    "UserTypeConst": "PAR"
}]

}

为什么要将NSMutableString转换为NSData

您可以直接将NSMutableString转换为NSDictionary

NSDictionary *json1 = [NSJSONSerialization JSONObjectWithData:[json dataUsingEncoding:NSUTF8StringEncoding] options:0 error:nil];
NSLog(@"JSON %@",json1);

试试这个:

NSString* string = [[[[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding] stringByReplacingOccurrencesOfString:@"t" withString:@""] stringByReplacingOccurrencesOfString:@"" withString:@""];
data = [string dataUsingEncoding:NSUTF8StringEncoding];
- (NSDictionary *)getJsonObjectfromString:(NSString*)jsonString{
    NSData *data = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
    NSError *error;
    id json = [NSJSONSerialization JSONObjectWithData:data
                                              options:0
                                                error:&error];
    return json;
}

是的,我得到了答案,请检查我的代码

NSData *data=[json dataUsingEncoding:NSUTF8StringEncoding];
NSError *error=nil;
NSDictionary *json1=[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];

tableArray= [NSMutableArray array];
menuArray=[NSMutableArray array];
for (NSDictionary *dic in [json1 objectForKey:@"Table"])
{
    [tableArray addObject:@{@"StudentId" : [dic objectForKey:@"StudentId"],
                         @"UserID" : [dic objectForKey:@"UserID"],
                         @"StudentName" : [dic objectForKey:@"StudentName"],
                         @"UserTypeName" : [dic objectForKey:@"UserTypeName"],
                         @"Path" : [dic objectForKey:@"Path"],
                         @"UnreadMessageCount" : [dic objectForKey:@"UnreadMessageCount"],
                         @"LastMessageTime" : [dic objectForKey:@"LastMessageTime"],
                         @"Image" : [dic objectForKey:@"Image"]}];
}
NSLog(@"table Array is==%@",tableArray);
for (NSDictionary *dic in [json1 objectForKey:@"Table1"])
{
    [menuArray addObject:@{@"MenuName":[dic objectForKey:@"MenuName"],
                           @"MenuName1" : [dic objectForKey:@"MenuName1"],
                           @"Icon" : [dic objectForKey:@"Icon"],
                           @"URL" : [dic objectForKey:@"URL"],
                           @"UserTypeConst" : [dic objectForKey:@"UserTypeConst"]}];
}
 //   [self performSegueWithIdentifier:@"loginbutton" sender:nil];
NSLog(@"menu count=%lu",(unsigned long)menuArray.count);
NSLog(@"Menu Array is==%@",menuArray);

删除网络响应中的空间后。

相关内容

  • 没有找到相关文章

最新更新