【问题标题】:How to parse this kind of json String in ios如何在ios中解析这种json字符串
【发布时间】:2015-12-16 13:15:35
【问题描述】:

您好,我是 ios 编程和学习的新手,我正在通过进行 web 服务调用和以 json 格式获取响应。我成功了,但我不知道如何获取(解析)响应并循环响应。我的响应如下,我的字典如下: json

{
    "contacts": [
        {
                "id": "c200",
                "name": "Ravi Tamada",
                "email": "ravi@gmail.com",
                "address": "xx-xx-xxxx,x - street, x - country",
                "gender" : "male",
                "phone": {
                    "mobile": "+91 0000000000",
                    "home": "00 000000",
                    "office": "00 000000"
                }
        },
        {
                "id": "c201",
                "name": "Johnny Depp",
                "email": "johnny_depp@gmail.com",
                "address": "xx-xx-xxxx,x - street, x - country",
                "gender" : "male",
                "phone": {
                    "mobile": "+91 0000000000",
                    "home": "00 000000",
                    "office": "00 000000"
                }
        },
        {
                "id": "c202",
                "name": "Leonardo Dicaprio",
                "email": "leonardo_dicaprio@gmail.com",
                "address": "xx-xx-xxxx,x - street, x - country",
                "gender" : "male",
                "phone": {
                    "mobile": "+91 0000000000",
                    "home": "00 000000",
                    "office": "00 000000"
                }
        },
        {
                "id": "c203",
                "name": "John Wayne",
                "email": "john_wayne@gmail.com",
                "address": "xx-xx-xxxx,x - street, x - country",
                "gender" : "male",
                "phone": {
                    "mobile": "+91 0000000000",
                    "home": "00 000000",
                    "office": "00 000000"
                }
        },
        {
                "id": "c204",
                "name": "Angelina Jolie",
                "email": "angelina_jolie@gmail.com",
                "address": "xx-xx-xxxx,x - street, x - country",
                "gender" : "female",
                "phone": {
                    "mobile": "+91 0000000000",
                    "home": "00 000000",
                    "office": "00 000000"
                }
        },
        {
                "id": "c205",
                "name": "Dido",
                "email": "dido@gmail.com",
                "address": "xx-xx-xxxx,x - street, x - country",
                "gender" : "female",
                "phone": {
                    "mobile": "+91 0000000000",
                    "home": "00 000000",
                    "office": "00 000000"
                }
        },
        {
                "id": "c206",
                "name": "Adele",
                "email": "adele@gmail.com",
                "address": "xx-xx-xxxx,x - street, x - country",
                "gender" : "female",
                "phone": {
                    "mobile": "+91 0000000000",
                    "home": "00 000000",
                    "office": "00 000000"
                }
        },
        {
                "id": "c207",
                "name": "Hugh Jackman",
                "email": "hugh_jackman@gmail.com",
                "address": "xx-xx-xxxx,x - street, x - country",
                "gender" : "male",
                "phone": {
                    "mobile": "+91 0000000000",
                    "home": "00 000000",
                    "office": "00 000000"
                }
        },
        {
                "id": "c208",
                "name": "Will Smith",
                "email": "will_smith@gmail.com",
                "address": "xx-xx-xxxx,x - street, x - country",
                "gender" : "male",
                "phone": {
                    "mobile": "+91 0000000000",
                    "home": "00 000000",
                    "office": "00 000000"
                }
        },
        {
                "id": "c209",
                "name": "Clint Eastwood",
                "email": "clint_eastwood@gmail.com",
                "address": "xx-xx-xxxx,x - street, x - country",
                "gender" : "male",
                "phone": {
                    "mobile": "+91 0000000000",
                    "home": "00 000000",
                    "office": "00 000000"
                }
        },
        {
                "id": "c2010",
                "name": "Barack Obama",
                "email": "barack_obama@gmail.com",
                "address": "xx-xx-xxxx,x - street, x - country",
                "gender" : "male",
                "phone": {
                    "mobile": "+91 0000000000",
                    "home": "00 000000",
                    "office": "00 000000"
                }
        },
        {
                "id": "c2011",
                "name": "Kate Winslet",
                "email": "kate_winslet@gmail.com",
                "address": "xx-xx-xxxx,x - street, x - country",
                "gender" : "female",
                "phone": {
                    "mobile": "+91 0000000000",
                    "home": "00 000000",
                    "office": "00 000000"
                }
        },
        {
                "id": "c2012",
                "name": "Eminem",
                "email": "eminem@gmail.com",
                "address": "xx-xx-xxxx,x - street, x - country",
                "gender" : "male",
                "phone": {
                    "mobile": "+91 0000000000",
                    "home": "00 000000",
                    "office": "00 000000"
                }
        }
    ]
}



 **code**

 NSString *mydata = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];

    NSLog(@"====My data from webservice is===%@",mydata);


     NSError *e = nil;
    //saving data in dictionary.
    NSDictionary *recipeDictionary = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&e];
        NSLog(@"My Dictionary is %@", recipeDictionary);

【问题讨论】:

  • 你试过搜索这个网站吗?

标签: ios json parsing


【解决方案1】:

很简单。

根据您的回复,确认您的回复是字典。在您的 recipeDictionary 中有一个与联系人键关联的数组对象。

所以在数组中取这个值。像这样

 NSArray *yourContactsArray = [recipeDictionary valueForKey:@"contacts"];

现在这个数组包含多个字典形式的对象。我正在打印 name 键中的所有值来演示你获取。

for (NSDictionary *dict in yourContactsArray)
{
     NSString *name = [dict valueForKey:@"name"];
     NSLog(@"%@",name);
}

【讨论】:

  • 如果您有任何问题,请询问
  • 除非您真的需要 KVC 功能,否则切勿使用 valueForKey: / setValue:forKey:。通过键获取值的专用方法是objectForKey:
  • 嗨..有一个问题
  • 我想使用 segue 将值传递到情节提要中的另一个屏幕,但我不能。请帮助我。
  • 你想在 storyboard 中传递值。 . .那么你面临什么问题,告诉我
【解决方案2】:

我建议您创建一个模型类来存储来自 Web 服务的数据。将类命名为 Contact

Contact.h

@interface Contact : NSObject

@property(nonatomic, strong) NSString *contactId;
@property(nonatomic, strong) NSString *name;
@property(nonatomic, strong) NSString *email;
@property(nonatomic, strong) NSString *address;
@property(nonatomic, strong) NSString *gender;
@property(nonatomic, strong) NSString *mobilePhone;
@property(nonatomic, strong) NSString *homePhone;
@property(nonatomic, strong) NSString *officePhone;

@end

在您的 Contact.m 文件中

@implementation Contacts



- (id)initWithDictionary:(NSMutableDictionary *)dict{

    self = [super init];
    if(self) {
        //Parse your dictionary into objects here
        _contactId = [dict objectForKey:@"id"];
        _email = [dict objectForKey:@"email"];
        _name = [dict objectForKey:@"name"];
        _address = [dict objectForKey:@"address"];
        _gender = [dict objectForKey:@"gender"];
        _homePhone = [[dict objectForKey:@"phone"] objectForKey:@"home"];
        _mobilePhone = [[dict objectForKey:@"id"] objectForKey:@"mobile"];
        _officePhone = [[dict objectForKey:@"id"] objectForKey:@"office"];
    }
    return self;
}

@end

在你的课堂上

NSString *mydata = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];

NSLog(@"====My data from webservice is===%@",mydata);
NSError *e = nil;
//saving data in dictionary.
NSDictionary *recipeDictionary = [NSJSONSerialization      JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&e];

NSLog(@"My Dictionary is %@", recipeDictionary);
NSMutableArray *contacts = [[NSMutableArray alloc] init];
for (NSDictionary *contactDict in  [recipeDictionary objectForKey:@"contacts"]) {
     Contact *contact = [[Contact alloc] initWithDictionary:contactDict];
      NSLog(@"Name %@",contact.name);  //Prints Name
     [contacts addObject: contact];
}

contacts 数组将包含联系人对象数组。您可以使用点符号来提取特定信息。
您可以将联系人数组传递给应用程序的任何部分。对象比字典好

【讨论】:

  • 这是一个很好的建议;但是我不认为 OP 已经到了拥有NSDictionary 的阶段;他想知道如何在 Objective-C 中解析 JSON(我相信,无论如何)。
【解决方案3】:

您应该做的主要事情是将 JSON 响应转换为 NSString/String 我建议使用这些库来解析 JSON。

Objective-C 版本:

https://github.com/TouchCode/TouchJSON

Swift 版本:

https://github.com/SwiftyJSON/SwiftyJSON

【讨论】:

  • 你为什么推荐使用这些库而不是内置库?
  • 因为内置的NSJSONSerialization 不像这些库那样方便,原因有很多。比如nil处理,类型检测
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-05-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多