Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 使用NSKeyValueCoding将复杂的JSON API响应消费到NSObject中_Iphone_Ios_Json_Nsobject_Key Value Coding - Fatal编程技术网

Iphone 使用NSKeyValueCoding将复杂的JSON API响应消费到NSObject中

Iphone 使用NSKeyValueCoding将复杂的JSON API响应消费到NSObject中,iphone,ios,json,nsobject,key-value-coding,Iphone,Ios,Json,Nsobject,Key Value Coding,这篇博文提供了一些将JSON响应转换为核心数据实体的好技巧。我想做的是更具体一点。我想获取一个JSON响应,并使用博客文章中的方法将对象转换为一个NSObject,其属性表示响应对象。我遇到的问题是对象的嵌套属性。以这个JSON响应为例: 使用博客文章中描述的方法,像“name”和“level”这样的简单属性很容易转换为NSString和NSNumber对象。但是,当查看响应中更复杂的部分时,就会出现问题:嵌套数组/字典 我发现的唯一解决方案是手工编写查找和转换所有这些属性的代码,我觉得这是一

这篇博文提供了一些将JSON响应转换为核心数据实体的好技巧。我想做的是更具体一点。我想获取一个JSON响应,并使用博客文章中的方法将对象转换为一个NSObject,其属性表示响应对象。我遇到的问题是对象的嵌套属性。以这个JSON响应为例:

使用博客文章中描述的方法,像“name”和“level”这样的简单属性很容易转换为NSString和NSNumber对象。但是,当查看响应中更复杂的部分时,就会出现问题:嵌套数组/字典

我发现的唯一解决方案是手工编写查找和转换所有这些属性的代码,我觉得这是一个非常糟糕的做法。以下是我所做工作的摘录:

    NSDictionary *skillsDictionary = json[@"skills"];
    if ([skillsDictionary isKindOfClass:[NSDictionary class]]) {
        NSArray *activeArray = skillsDictionary[@"active"];
        NSArray *passiveArray = skillsDictionary[@"passive"];

        NSMutableArray *mutActives = [NSMutableArray array];
        NSMutableArray *mutPassives = [NSMutableArray array];

        if ([activeArray isKindOfClass:[NSArray class]]) {
            [activeArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
                if ([obj isKindOfClass:[NSDictionary class]]) {
                    NSDictionary *activeJSON = (NSDictionary*)obj;
                    D3Skill *skill = [D3Skill activeSkillFromJSON:activeJSON];
                    if (skill) {
                        [mutActives addObject:skill];
                    }
                }
            }];
        }

        if ([passiveArray isKindOfClass:[NSArray class]]) {
            [passiveArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
                if ([obj isKindOfClass:[NSDictionary class]]) {
                    NSDictionary *passiveJSON = (NSDictionary*)obj;
                    D3Skill *skill = [D3Skill passiveSkillFromJSON:passiveJSON];
                    if (skill) {
                        [mutPassives addObject:skill];
                    }
                }
            }];
        }

        self.activeSkills = mutActives;
        self.passiveSkills = mutPassives;
    }

我使用SBJSON库和ASIHttpRequest从我自己设计的Web服务中获取和使用JSON,如果我正确理解您的问题,您只需要执行以下操作:

NSString *responseJSONasString = [fetchRequest responseString]; 
NSDictionary *itemResponseArray = [responseJSONasString JSONValue];
...
NSString *jsonString = @"{your-json-string}";
YourValueObject *dataModel = [YourValueObject fromJSONString:jsonString];

NSDictionary *jsonObject = @{your-json-object};
YourValueObject *dataModel = [YourValueObject fromJSONObject:jsonObject];
...
YourValueObject *dataModel = instance-of-your-value-object;
NSString *jsonString = [dataModel toJSONString];
NSDictionary *jsonObject = [dataModel toJSONObject];
...
SBJSON库将为您将转换为NSObject,并使用JSON元素上的key:value编码将它们放入数组/字典中,因此使用您提供的JSON,我可以使用以下代码获得第一个英雄的名字:

NSArray *heroes = [itemResponseArray objectForKey:@"heroes"];
NSDictionary *firstHero = [heroes objectAtIndex:0];
NSString *heroName = [firstHero objectForKey:"name"];

SBJson可以在这里找到:

我使用SBJson库和ASIHttpRequest从我自己设计的Web服务中获取和使用JSON,如果我正确理解了您的问题,您只需要执行以下操作:

NSString *responseJSONasString = [fetchRequest responseString]; 
NSDictionary *itemResponseArray = [responseJSONasString JSONValue];
...
NSString *jsonString = @"{your-json-string}";
YourValueObject *dataModel = [YourValueObject fromJSONString:jsonString];

NSDictionary *jsonObject = @{your-json-object};
YourValueObject *dataModel = [YourValueObject fromJSONObject:jsonObject];
...
YourValueObject *dataModel = instance-of-your-value-object;
NSString *jsonString = [dataModel toJSONString];
NSDictionary *jsonObject = [dataModel toJSONObject];
...
SBJSON库将为您将转换为NSObject,并使用JSON元素上的key:value编码将它们放入数组/字典中,因此使用您提供的JSON,我可以使用以下代码获得第一个英雄的名字:

NSArray *heroes = [itemResponseArray objectForKey:@"heroes"];
NSDictionary *firstHero = [heroes objectAtIndex:0];
NSString *heroName = [firstHero objectForKey:"name"];
SBJson可以在这里找到:

是一个轻量级库,我在项目中使用它将JSON数据从RESTful api转换为业务数据模型。事情是这样的:

NSString *responseJSONasString = [fetchRequest responseString]; 
NSDictionary *itemResponseArray = [responseJSONasString JSONValue];
...
NSString *jsonString = @"{your-json-string}";
YourValueObject *dataModel = [YourValueObject fromJSONString:jsonString];

NSDictionary *jsonObject = @{your-json-object};
YourValueObject *dataModel = [YourValueObject fromJSONObject:jsonObject];
...
YourValueObject *dataModel = instance-of-your-value-object;
NSString *jsonString = [dataModel toJSONString];
NSDictionary *jsonObject = [dataModel toJSONObject];
...
是一个轻量级库,我在项目中使用它将JSON数据从RESTful api转换为业务数据模型。事情是这样的:

NSString *responseJSONasString = [fetchRequest responseString]; 
NSDictionary *itemResponseArray = [responseJSONasString JSONValue];
...
NSString *jsonString = @"{your-json-string}";
YourValueObject *dataModel = [YourValueObject fromJSONString:jsonString];

NSDictionary *jsonObject = @{your-json-object};
YourValueObject *dataModel = [YourValueObject fromJSONObject:jsonObject];
...
YourValueObject *dataModel = instance-of-your-value-object;
NSString *jsonString = [dataModel toJSONString];
NSDictionary *jsonObject = [dataModel toJSONObject];
...

一个小注意事项是,我注意到在尝试从ASIHTTPRequest获取responseString的JSONValue时出现了错误。为了谨慎起见,我现在自己对字符串进行编码:NSString*responseJSONasString=[[NSString alloc]initWithData:[fetchRequest-responseData]encoding:NSUTF8StringEncoding];NSDictionary*itemResponseArray=[responseJSONasString JSONValue];[响应字符串释放];一个小注意事项是,我注意到在尝试从ASIHTTPRequest获取responseString的JSONValue时出现了错误。为了谨慎起见,我现在自己对字符串进行编码:NSString*responseJSONasString=[[NSString alloc]initWithData:[fetchRequest-responseData]encoding:NSUTF8StringEncoding];NSDictionary*itemResponseArray=[responseJSONasString JSONValue];[响应字符串释放];