Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/44.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 如何从Objective-C中的Json响应中获取值_Iphone_Ios_Objective C_Json_Ios5 - Fatal编程技术网

Iphone 如何从Objective-C中的Json响应中获取值

Iphone 如何从Objective-C中的Json响应中获取值,iphone,ios,objective-c,json,ios5,Iphone,Ios,Objective C,Json,Ios5,我在从Json响应中获取数据时遇到问题 以下是一个示例数据结构: ( { AT = "<null>"; DId = 0; DO = 0; PLId = 33997; PdCatList = ( { PLId = 33997; PPCId = 0; pd

我在从Json响应中获取数据时遇到问题

以下是一个示例数据结构:

(
     {
        AT = "<null>";
        DId = 0;
        DO = 0;
        PLId = 33997;
        PdCatList =  (
                       {
                PLId = 33997;
                PPCId = 0;

                pdList = (
                      {
                        IsDis = 0;
                        IsPS = 0;
                        IsT = 1;
                        PA = 1;
                        PCId = 119777;
                     }
                );
            }
        );
        PdId = 0;
        SId = 0;
        Sec = 1;
    },
     {
        AT = "<null>";
        DId = 0;
        DO = 11;
        Dis = 0;
        PLId = 34006;
        PdCatList =   (
                {

                PLId = 34006;
                PPCId = 0;
                pdList =  (
                       {
                        IsDis = 0;
                        IsPS = 0;
                        IsT = 1;
                        PA = 1;
                        PCId = 119830;
                       },
                       {
                        IsDis = 0;
                        IsPS = 0;
                        IsT = 1;
                        PA = 1;
                        PCId = 119777;
                       }
                   );
              },

             {
                PLId = 33997;
                PPCId = 0;
                pdList = (
                      {
                        IsDis = 0;
                        IsPS = 0;
                        IsT = 1;
                        PA = 1;
                        PCId = 119777;
                     }
                );
            }

        );
        PdId = 0;
        SId = 0;
        Sec = 1;
    },
)
(
{
AT=“”;
DId=0;
DO=0;
PLId=33997;
PdCatList=(
{
PLId=33997;
PPCId=0;
pdList=(
{
IsDis=0;
IsPS=0;
IsT=1;
PA=1;
PCId=119777;
}
);
}
);
PdId=0;
SId=0;
秒=1;
},
{
AT=“”;
DId=0;
DO=11;
Dis=0;
PLId=34006;
PdCatList=(
{
PLId=34006;
PPCId=0;
pdList=(
{
IsDis=0;
IsPS=0;
IsT=1;
PA=1;
PCId=119830;
},
{
IsDis=0;
IsPS=0;
IsT=1;
PA=1;
PCId=119777;
}
);
},
{
PLId=33997;
PPCId=0;
pdList=(
{
IsDis=0;
IsPS=0;
IsT=1;
PA=1;
PCId=119777;
}
);
}
);
PdId=0;
SId=0;
秒=1;
},
)
如何解析结果结构? 我想直接得到一个值列表。如果一个元组中有几个值,例如performer PdCatList、pdList,该怎么办。我如何访问这些值? 有人能帮我吗

谢谢

我的代码是

NSError *error;
    Array1 = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error];

    for(int i=0;i<[Array1 count];i++)
    {
        NSDictionary *dict1 = [Array1 objectAtIndex:i];

        NSLog(@"Array1.....%@",dict1);

        Array2=[dict1 valueForKey:@"PdCatList"];

        for(int i=0;i<[Array2 count];i++)
        {

            NSDictionary *dict2 = [Array2 objectAtIndex:i];

            NSLog(@"Array2.....%@",dict2);

            Array3=[dict2 valueForKey:@"pdList"];

            for(int i=0;i<[Array3 count];i++)
            {

                NSDictionary *dict3 = [Array3 objectAtIndex:i];

                NSLog(@"Array3.....%@",dict3);

            }


        }


    }
NSError*错误;
Array1=[NSJSONSerialization JSONObjectWithData:responseData选项:针织错误:&错误];

对于(inti=0;i,我认为这里需要的是理解JSON响应是什么,而不是从JSON响应中获取某些对象的值

如果你想要一些关于JSON解析的详细解释,那么你可以看一看。这里提供了所有信息,或者你可以看一看我的


理解这个概念。这取决于您的
JSON
中包含的内容。如果它是数组(值在
[]
中),那么您必须保存在
NSArray
中,如果它是字典(值在
{}
中)然后另存为
NSDictionary
,如果您有单值,如string、integer、double,则必须使用适当的Objective-C数据类型保存它们

有关示例的一些简单细节,您可以从这个问题中查看my。

使用JSONKit()


您必须添加JSON框架,将字符串解析到NSDictionary中。 使用来自的zip文件

  • 打开文件夹并将Classes文件夹重命名为“JSON”
  • 复制JSON文件夹,并将其包含在项目中
  • 导入头文件类似于控制器中的以下文件,您要在其中解析JSON字符串

    #导入“SBJSON.h”
    #导入“NSString+SBJSON.h”
    
  • 现在,将响应字符串解析到NSDictionary,如下所示

    NSMutableDictionary*dictResponse=[strResponse JSONValue];
    
  • 试试这个

    NSError *error;
    Array1 = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error];
    
    for(int i=0;i<[Array1 count];i++)
    {
        NSDictionary *dict1 = [Array1 objectAtIndex:i];
    
        ATArray =[dict1 valueForKey:@"AT"];
        DIdArray =[dict1 valueForKey:@"DId"];
        DOArray =[dict1 valueForKey:@"DO"];
        PLIdArray =[dict1 valueForKey:@"PLId"];
    
        etc...
    
        Array2=[dict1 valueForKey:@"PdCatList"];
    
        for(int i=0;i<[Array2 count];i++)
        {
    
            NSDictionary *dict2 = [Array2 objectAtIndex:i];
    
            PLIdArray =[dict2 valueForKey:@"PLId"];
            PPCIdArray =[dict2 valueForKey:@"PPCId"];
    
            etc…
    
            Array3=[dict2 valueForKey:@"pdList"];
    
            for(int i=0;i<[Array3 count];i++)
            {
    
                NSDictionary *dict3 = [Array3 objectAtIndex:i];
    
                IsDisArray =[dict3 valueForKey:@"IsDis"];
                IsPSArray =[dict3 valueForKey:@"IsPS"];
                IsTArray =[dict3 valueForKey:@"IsT"];
                PAArray =[dict3 valueForKey:@"PA"];
                PCIdArray =[dict3 valueForKey:@"PCId"];
            }
        }
    }
    
    NSError*错误;
    Array1=[NSJSONSerialization JSONObjectWithData:responseData选项:针织错误:&错误];
    对于(int i=0;i您可以使用
    访问JSON中的嵌套属性。您需要了解和

    将JSON映射到对象的框架,例如严重依赖KVC的框架

    根据示例,您可以获得所有PdCatList对象的列表:

    //sample data
    NSArray *json = @[
                      @{@"PLId" : @33997,
                        @"PdCatList" : @{@"PLId": @33998,
                                         @"PPCId" : @1,
                                        @"pdList" : @{
                                          @"PCId" : @119777
                                          }}
                            },
                      @{@"PLId" : @33999,
                        @"PdCatList" : @{@"PLId": @4444,
                                         @"PPCId" : @0,
                                         @"pdList" : @{
                                                 @"PCId" : @7777
                                                 }}}
                        ];
    
    //KVC
    NSArray *pdCatLists = [json valueForKeyPath:@"@unionOfObjects.PdCatList"];
    
    例如,有了它,您可以创建一个非常基本的对象映射(不考虑关系)

    在PdCatList.h中

    @interface PdCatList : NSObject
    @property (readonly, strong, nonatomic) NSNumber *PLId;
    @property (readonly, strong, nonatomic) NSNumber *PPCId;
    + (instancetype)listWithDictionary:(NSDictionary *)aDictionary;
    @end
    
    @interface NSJSONSerialization (FlattenedJSON)
    + (void)FlattenedJSONObjectWithData:(NSData *)data completionSuccessBlock:(void(^)(id aJson))onSuccess failure:(void(^)(NSError *anError))onFailure;
    @end
    
    在PdCatList.m中

    @implementation PdCatList
    - (void)setValue:(id)value forUndefinedKey:(NSString *)key
    {
        @try {
            [super setValue:value forUndefinedKey:key];
        }
        @catch (NSException *exception) {
            NSLog(@"error setting undefined key: %@, exception: %@", key, exception);
        };
    }
    + (id)listWithDictionary:(NSDictionary *)aDictionary
    {
        PdCatList *result = [[self alloc] init];
        [result setValuesForKeysWithDictionary:aDictionary];
        return result;
    }
    @end
    
    #import "NSJSONSerialization+FlattenedJSON.h"
    
    @implementation NSJSONSerialization (FlattenedJSON)
    + (void)FlattenedJSONObjectWithData:(NSData *)data completionSuccessBlock:(void (^)(id))onSuccess failure:(void (^)(NSError *))onFailure
    {
        NSError *error;
        id object = [self JSONObjectWithData:data
                         options:kNilOptions
                           error:&error];
        if (error)
        {
            onFailure(error);
        }
        else
        {
            NSMutableArray *result = [NSMutableArray array];
            [self flatten:object
                  inArray:result];
            onSuccess([result copy]);
        }
    }
    + (void)flatten:(id)anObject inArray:(NSMutableArray *)anArray
    {
        if ([anObject isKindOfClass:NSDictionary.class])
        {
            [((NSDictionary *)anObject) enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
                [self flatten:obj inArray:anArray];
            }];
        }
        else if ([anObject isKindOfClass:NSArray.class])
        {
            [((NSArray *)anObject) enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
                [self flatten:obj inArray:anArray];
            }];
        }
        else
        {
            [anArray addObject:anObject];
        }
    }
    @end
    
    获取json对象后

    NSArray *pdCatLists = [json valueForKeyPath:@"@unionOfObjects.PdCatList"];
    
    [pdCatLists enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        PdCatList *each = [PdCatList listWithDictionary:obj];
    }];
    
    但是,如果您只想展平json,则必须使用递归并创建类似于以下内容的类别

    在NSJSONSerialization+flattedjson.h中

    @interface PdCatList : NSObject
    @property (readonly, strong, nonatomic) NSNumber *PLId;
    @property (readonly, strong, nonatomic) NSNumber *PPCId;
    + (instancetype)listWithDictionary:(NSDictionary *)aDictionary;
    @end
    
    @interface NSJSONSerialization (FlattenedJSON)
    + (void)FlattenedJSONObjectWithData:(NSData *)data completionSuccessBlock:(void(^)(id aJson))onSuccess failure:(void(^)(NSError *anError))onFailure;
    @end
    
    在NSJSONSerialization+flattedjson.m中

    @implementation PdCatList
    - (void)setValue:(id)value forUndefinedKey:(NSString *)key
    {
        @try {
            [super setValue:value forUndefinedKey:key];
        }
        @catch (NSException *exception) {
            NSLog(@"error setting undefined key: %@, exception: %@", key, exception);
        };
    }
    + (id)listWithDictionary:(NSDictionary *)aDictionary
    {
        PdCatList *result = [[self alloc] init];
        [result setValuesForKeysWithDictionary:aDictionary];
        return result;
    }
    @end
    
    #import "NSJSONSerialization+FlattenedJSON.h"
    
    @implementation NSJSONSerialization (FlattenedJSON)
    + (void)FlattenedJSONObjectWithData:(NSData *)data completionSuccessBlock:(void (^)(id))onSuccess failure:(void (^)(NSError *))onFailure
    {
        NSError *error;
        id object = [self JSONObjectWithData:data
                         options:kNilOptions
                           error:&error];
        if (error)
        {
            onFailure(error);
        }
        else
        {
            NSMutableArray *result = [NSMutableArray array];
            [self flatten:object
                  inArray:result];
            onSuccess([result copy]);
        }
    }
    + (void)flatten:(id)anObject inArray:(NSMutableArray *)anArray
    {
        if ([anObject isKindOfClass:NSDictionary.class])
        {
            [((NSDictionary *)anObject) enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
                [self flatten:obj inArray:anArray];
            }];
        }
        else if ([anObject isKindOfClass:NSArray.class])
        {
            [((NSArray *)anObject) enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
                [self flatten:obj inArray:anArray];
            }];
        }
        else
        {
            [anArray addObject:anObject];
        }
    }
    @end
    

    我看到了你的问题,发现你总是为每一个新的
    JSON
    响应提出一个新问题。for(int I=0;我看一下答案:。这可能会帮助你理解什么是JSON解析。亲爱的编辑和编辑审阅者,我刚刚了解了这一点,但你必须