-9

我从 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;
    },
)

我将如何解析结果结构?我想直接获取值列表。如果我在元组中有多个值,例如执行者 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);

            }


        }


    }
4

6 回答 6

3

尝试这个...

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"];
        }
    }
}
于 2013-08-07T17:17:27.967 回答
2

我认为您在这里需要的是了解 JSON 响应是什么,而不是从您的 JSON 响应中获取某些对象的值的答案。

如果您想了解有关 JSON 解析的详细说明,可以查看NSJSONSerialization Class Reference。一切都在那里给出,或者你可以看看我的答案


理解概念。这取决于你的JSON. 如果它是一个数组(里面的值[ ])那么你必须保存NSArray,如果它是一个字典(里面的值{ })然后保存为NSDictionary,如果你有字符串、整数、双精度等单个值,那么你必须使用适当的 Objective-C 数据保存它们类型。

有关示例的一些简单详细信息,您可以查看我对此问题的回答

于 2013-08-03T19:42:19.143 回答
1

使用 JSONKit(https://github.com/johnezang/JSONKit):

NSString *yourJSONString = ...
NSArray *responseArray = [yourJSONString objectFromJSONString];
for(NSDictionary *responseDictionary in responseArray)
{
    NSString *atString = [responseDictionary objectForKey:@"AT"];
    ...
    NSArray *pdCatListArray = [responseDictionary objectForKey:@"PdCatList"];
    ...here you can get all values you want,if you want to get more details in PdCatList,use for in pdCatListArray ,you can do what you want.
}
于 2013-08-06T05:57:17.543 回答
1
Use following method:


NSDictionary *mainDict;
SBJSON *jsonParser = [[SBJSON alloc]init];
    if([[jsonParser objectWithString:responseString] isKindOfClass:[NSDictionary class]])
    {
        mainDict=[[NSDictionary alloc]initWithDictionary:[jsonParser objectWithString:responseString]];
    }

NSDictionary *firstDict=[NSDictionary alloc]initWithDictionary:[mainDict valueForKey:@""];
于 2013-08-06T09:50:51.617 回答
0

您可以使用KVC 访问 JSON 中的嵌套属性。您需要了解KVC 和点语法以及集合运算符

将 JSON 映射到对象的框架(例如RestKit)严重依赖 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

在 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

拿到json对象后

NSArray *pdCatLists = [json valueForKeyPath:@"@unionOfObjects.PdCatList"];

[pdCatLists enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
    PdCatList *each = [PdCatList listWithDictionary:obj];
}];

但是,如果您想要的只是扁平化 json,则必须使用递归并创建类似于以下的类别。

在 NSJSONSerialization+FlattenedJSON.h

@interface NSJSONSerialization (FlattenedJSON)
+ (void)FlattenedJSONObjectWithData:(NSData *)data completionSuccessBlock:(void(^)(id aJson))onSuccess failure:(void(^)(NSError *anError))onFailure;
@end

在 NSJSONSerialization+FlattenedJSON.m

#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
于 2013-08-08T10:57:33.840 回答
0

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

  1. 打开文件夹并将 Classes 文件夹重命名为“JSON”。
  2. 复制JSON 文件夹并包含在您的项目中。
  3. 在要解析 JSON 字符串的控制器中导入如下所示的头文件。

    #import "SBJSON.h"
    #import "NSString+SBJSON.h"
    
  4. 现在,将您的响应字符串解析为NSDictionary,如下所示。

    NSMutableDictionary *dictResponse = [strResponse JSONValue];
    
于 2013-08-06T12:14:50.243 回答