在Objective C中使用RestKit解析复杂的JSON [英] Parsing complex JSON using RestKit in Objective C

查看:111
本文介绍了在Objective C中使用RestKit解析复杂的JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

几天来我一直在遇到这个问题,如果有人能够为此提供一些指导甚至完整的代码解决方案,我将非常感激. 我正在尝试使用RestKit在Objective C中执行到JSON字符串的动态对象映射,但似乎无法获得正确的值. 这是我需要解析的JSON响应类型的示例:

I've been having issues with this issue for several days now, and would really appreciate it if someone will be able to provide some guidance or even a full code solution for this. I'm trying to perform dynamic object mapping to a JSON string in Objective C using RestKit and can't seem to get the right values. This is an example to the kind of JSON response I need to parse:

{
    "Boy" :
    {
        "favoriteClass" : "math",
        "basicInfo" :
        {
           "name" : "John",
           "age"  : 10,
           "type" : 1
        }
        "friends" :
        [
           {
              "Boy" :
              {
                 "favoriteClass" : "PE"
                 "basicInfo" :
                 {
                    "name" : "Bill",
                    "age" : 12,
                    "type" : 1
                 }
                 "friends" : []
              },
              "Girl" :
              {
                 "favoriteTeacher" : "Mrs. Manson"
                 "basicInfo" :
                 {
                    "name" : "Sara",
                    "age" : 11,
                    "type" : 2
                 }
                 "friends" : []
              },
              "Girl" :
              {
                 "favoriteTeacher" : "Mr. Chase"
                 "basicInfo" :
                 {
                    "name" : "Ronda",
                    "age" : 9,
                    "type" : 2
                 }
                 "friends" : []
              }
           }
        ]
    }
}

这意味着,我有两种类型的课程:购买课程和女孩课程. 它们每个都有不同的字段(男孩喜欢的类是favoriteClass,女孩喜欢的类是favouriteTeacher),但是两者都有一个basicInfo字段,其中包含完全相同的结构. 我可以使用数组中记录的名称(男孩"或女孩")或通过类型"字段中的值来判断哪个应该映射到Boy类,哪个应该映射到Girl类. "basicInfo"记录(男孩为1,女孩为2). 男孩"和女孩"类的friends数组可以包含男孩和女孩的实例.

Meaning, I have two types of classes: a Buy class and a Girl class. They each have different fields (favoriteClass for the boys and favoriteTeacher for the girls), but the both have a basicInfo field, which contains exactly the same structure. I can tell which should be mapped to the Boy class and which should be mapped to the Girl class with using the name of the record in the array ("Boy" or "Girl"), or by the "type" field's value within the "basicInfo" record (1 for boys and 2 for girls). The friends array for both a Boy and a Girl class can contain instances of both boys and girls.

任何人都可以给我一些有关如何完成此工作的指示吗? 任何帮助将不胜感激.

Can anybody please give me some pointers on how this can be done? Any help will be greatly appreciated.

谢谢

推荐答案

我今天才开始查看RestKit,所以我离权威机构很远.但是,查看Blake的出色文档,在我看来,您过于复杂了(您发布的JSON不能验证BTW).除非您特别需要BasicInfo对象,否则我将其删除并按以下方式配置您的JSON:

I just started looking at RestKit today, so I am far from an authority. However, looking at Blake's excellent documentation, it seems to me that you are overly complicating this (your posted JSON does not validate BTW). Unless you have a specific need to have a BasicInfo object, I'd remove it and configure your JSON thus:

{
    "people": [
        {
            "age": 10, 
            "favoriteClass": "math", 
            "name": "John", 
            "type": 1,
            "friends": [
                {
                    "age": 12, 
                    "favoriteClass": "PE", 
                    "friends": [], 
                    "name": "Bill", 
                    "type": 1
                }, 
                {
                    "age": 11, 
                    "favoriteTeacher": "Mrs. Manson", 
                    "friends": [], 
                    "name": "Sara", 
                    "type": 2
                }, 
                {
                    "age": 9, 
                    "favoriteTeacher": "Mr. Chase", 
                    "friends": [], 
                    "name": "Ronda", 
                    "type": 2
                }
            ] 
        }
    ]
}

然后,您应该能够使用他在动态对象映射部分中描述的方法(1). /master/Docs/Object%20Mapping.md"rel =" nofollow>对象映射文档.在我看来,与他给出的示例相比,您唯一的不同是您在其中具有一些附加的ivars.每个BoyGirl类,并且您使用数字而不是字符串来标识BoyGirl.您可以通过修改他给出的声明来解决该问题:

Then you should be able to utilize approach (1) that he describes in the Dynamic Object Mapping section of the Object Mapping document. It appears to me that the only difference you have in comparison with the example he gives is that you have some additional ivars in each of the Boy and Girl classes, and you are using an number rather than a string to identify Boy vs Girl. You can deal with that by modifying the declarations he gives to be:

// Basic setup
RKObjectMapping* boyMapping = [RKObjectMapping mappingForClass:[Boy class]];
[boyMapping mapAttributes:@"age", @"name", @"favoriteClass",nil];

RKObjectMapping* girlMapping = [RKObjectMapping mappingForClass:[Girl class]];
[girlMapping mapAttributes:@"age", @"name", @"favoriteTeacher", nil];

// Configure the dynamic mapping via matchers
[dynamicMapping setObjectMapping:boyMapping whenValueOfKeyPath:@"type" isEqualTo:@"1"];
[dynamicMapping setObjectMapping:girlMapping whenValueOfKeyPath:@"type" isEqualTo:@"2"];

匹配器的类型可能不太正确-正如我所说,我今天才开始阅读这些内容.

The type of the matcher may not be quite right -- as I said, I just started reading this stuff today.

这篇关于在Objective C中使用RestKit解析复杂的JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆