iOS:使用JSONModel,如何抓住和数字键字典吗? - iOS: Using JSONModel, how do you grab dictionaries with numeric key?

- 此内容更新于:2015-12-13
主题:

我目前使用由icanzilbJSONModel解析JSON结构获得在线。问题是,我有一个字典数字键,加上这些数字键按需生成的服务器。所以理论上我没有提前知道这些数字键。所以在这种情况下,我不能添加@propertyJSON数据头文件。知道如何处理这种情况吗?JSON结构的一个例子:

原文:

I'm currently using JSONModel by icanzilb to parse my JSON structures obtained online. The issue is that I have a dictionary which has numeric keys, plus these numeric keys are generated by the server on-demand. So theoretically I have no advance knowledge of what these numeric keys will be. So under this situation, I couldn't add a @property to my JSON data header files.

Any idea on how to deal with this situation?

An example of the JSON structure:

{
 "Content":[
             {
              "0":{...},
              "1":{...},
              "2":{...},
              "Forum":"1",
              "Member": "Michael",
             }
          ],
 "Count":"5"
}
网友:你为什么使用动态密钥?

(原文:Why are you using dynamic keys ?)

解决方案:
你不能将属性添加到一个本地对象动态基于JSON响应,但你可以有一个属性是一个称为unaryoperations填满,只有数字键/对象是动态的。然后可以使用获得与该密钥关联对象
原文:

You can't add properties to a native object dynamically based on the JSON response but you could have a property that is a NSMutableDictionary and fill that with only the numeric keys/objects that are dynamic.

-(void)fillMyDictionaryProperty {

    NSCharacterSet *nonNumericSet = [[NSCharacterSet decimalDigitCharacterSet] invertedSet];


    for (NSString *key in jsonDictionary.allKeys) {

        if ([key rangeOfCharacterFromSet:nonNumericSet].location == NSNotFound) {

            id object = [jsonDictionary objectForKey:key];
            [myMutableDictionary setObject:object forKey:key];

        }

    }

}

Then you could use this to get the object associated with that key

-(id)getPropertyWithNumber:(NSInteger)num {

    NSString *key = [NSString stringWithFormat:@"%li", (long)num];
    return [myMutableDictionary objectForKey:key];

}
解决方案:
第一把字典数组的内容。然后获取字典,你知道所有对在字典里除了最后两个数字键值。你可以得到数值的计算如下。后,你可以得到所有的数字键值在一个循环中。
原文:

First get the dictionary in Content array. Then get the count of dictionary, you know that all the pairs in the dictionary have numerical key values except last two. you can get the count of numerical values as follows.

NSArray *array = [jsonString valueForKey:@"Content"];
NSDictionary *dict = [array objectAtIndex:0];

//Count of numeric keys in dict

int count = [dict count]-2;

After that you can get all the numeric key values in a loop.

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

    NSDictionary *valueDict = [dict valueForKey:[NSString stringWithFormat:@"%d",i]];
}
楼主:谢谢你的快速回复。但是,这里的问题是“0”,“1”……是由服务器按需生成的。所以可能有情况只有“0”和“1”或实例哪里有“0”,“1”、“2”、“3”…“100”。所以我不能硬编码,对吧?

(原文:Thanks for the quick reply. However, the issue here is that the "0", "1"... are generated by server on-demand. So there could be instances where there is only "0" and "1" or instances where there is "0", "1", "2", "3"..."100". So I can't hard code them in, right?)

网友:是的,你不能硬编码。这完全取决于服务器响应。

(原文:Yes you cannot hardcode them. It totally depends on the server response.)

网友:对不起现在我理解你的问题。请检查更新。

(原文:Sorry I understand your problem now. Please check update.)

楼主:谢谢Fawad。这是我使用的方法在我切换到使用JSONModel库之前,如果在解析为其提供方便的目的,做没有意义。谢谢你的努力的!

(原文:Thanks Fawad. This is the method I use before I switch to using the JSONModel library, which if to serve its purpose of providing convenience in parsing, do not make sense doing the above. Thanks for the effort by the way!)