-1

Sudzc 生成的代码为反序列化节点编写了一个字典。如果我使用 NSLog(@"The Child Node: %@", [[[element children] objectAtIndex:0] stringValue]); 每次通过时,它都会写出正确的项目。当我尝试在代码中检索结果时,只有最后一个可用(Jackson 3)。我究竟做错了什么?

// Deserializes the element in a dictionary.
+(id)deserializeAsDictionary:(CXMLNode*)element {

if([element childCount] == 1) {
    CXMLNode* child = [[element children] objectAtIndex:0];
    if([child kind] == CXMLTextKind) 
    {
         NSLog(@"The Child Node: %@", [[[element children] objectAtIndex:0] stringValue]);
        return [[[element children] objectAtIndex:0] stringValue];

    }
}

NSMutableDictionary* d = [NSMutableDictionary dictionary];
for(CXMLNode* child in [element children]) {
    id v = [Soap deserialize:child];
    if(v == nil) { v = [NSNull null]; }
    [d setObject:v forKey:[child name]];
}
return d;
}

NS日志:

2012-04-19 14:13:07.802 Management[3043:10703] Hopefully Child: Allen
2012-04-19 14:13:07.803 Management[3043:10703] Hopefully Child: 1
2012-04-19 14:13:07.804 Management[3043:10703] Hopefully Child: John
2012-04-19 14:13:07.804 Management[3043:10703] Hopefully Child: 2
2012-04-19 14:13:07.805 Management[3043:10703] Hopefully Child: Jackson
2012-04-19 14:13:07.805 Management[3043:10703] Hopefully Child: 3

XML:

<TC diffgr:id="TC1" msdata:rowOrder="0">
 <CSHR_POS_NAME>Allen</CSHR_POS_NAME>                            
    <CSHR_NUM>66</CSHR_NUM>
</TC>

<TC diffgr:id="TC2" msdata:rowOrder="1">                                    
  <CSHR_POS_NAME>John</CSHR_POS_NAME>
    <CSHR_NUM>2</CSHR_NUM>
    </TC>

<TC diffgr:id="TC3" msdata:rowOrder="2">
<CSHR_POS_NAME>Jackson</CSHR_POS_NAME>
<CSHR_NUM>3</CSHR_NUM>
</TC>
4

5 回答 5

2

解决(改变了soap.m):

[d setObject:v forKey:[child name]]; 
NSString* key = [child name]; 
id check = [d objectForKey:key]; 
if( check != nil ) { 

    NSInteger next = 1; 
    key = [NSString stringWithFormat:@"%@%d", [child name], next]; 
    check = [d objectForKey:key]; 
    while( check != nil ) { 

        next++; 
        key = [NSString stringWithFormat:@"%@%d", [child name], next]; 
        check = [d objectForKey:key]; 
    } 
    [d setObject:v forKey:key]; 
} 
[d setObject:v forKey:[child name]]; 
于 2012-04-24T13:35:53.807 回答
1

我发现您还必须放入一个 else 块:

if( check != nil ) {             
    NSInteger next = 1;             
    key = [NSString stringWithFormat:@"%@%04d", [child name], next];
    check = [d objectForKey:key]; 
    while( check != nil ) {                 
        next++; 
        key = [NSString stringWithFormat:@"%@%04d", [child name], next]; 
        check = [d objectForKey:key]; 
    } 
    [d setObject:v forKey:key]; 
} else {
    [d setObject:v forKey:[child name]];
}
//End Extension

否则 'd' 中的元素将被覆盖,因为 setObject 被调用了两次。

于 2012-08-01T09:27:44.243 回答
1

一旦我得到足够的代表点,我会的,但我确实注意到代码的开头和结尾是

  [d setObject:v forKey:[child name]];

对我来说,我必须删除初始行,并为我修复它,所以代码如下所示:

// Deserializes the element in a dictionary.
 +(id)deserializeAsDictionary:(CXMLNode*)element {

if([element childCount] == 1) {
    CXMLNode* child = [[element children] objectAtIndex:0];
    if([child kind] == CXMLTextKind) {            
    return [[[element children] objectAtIndex:0] stringValue];
    }
}   
NSMutableDictionary* d = [NSMutableDictionary dictionary];

for(CXMLNode* child in [element children]) {

id v = [Soap deserialize:child];
if(v == nil) { v = [NSNull null]; }

//[d setObject:v forKey:[child name]]; //seems to be duped (maybe my bad)
    //Extended by iDev on StackOverflow
    //http://stackoverflow.com/questions/10235496/sudzc-deserializeasdictionary-over-written-dictionary/10358458#10358458
    NSString* key = [child name]; 
    id check = [d objectForKey:key]; 
    if( check != nil ) {             
        NSInteger next = 1;             
        key = [NSString stringWithFormat:@"%@%04d", [child name], next];
        check = [d objectForKey:key]; 
        while( check != nil ) {                 
            next++; 
            key = [NSString stringWithFormat:@"%@%04d", [child name], next]; 
            check = [d objectForKey:key]; 
        } 
        [d setObject:v forKey:key]; 
    } 
    [d setObject:v forKey:[child name]]; 
    //End Extension
}
return d;

}

于 2012-05-02T01:36:05.447 回答
0

我在上面的代码中遇到了问题——它每次都会覆盖第一个条目——即我会得到一个包含 4 个项目的列表,而第一个和第四个将被重复。

经过大量累人的代码(不喜欢递归代码),我发现了我认为的问题;

我的代码如下:

    if( check != nil ) {
        NSInteger next = 1;
        key = [NSString stringWithFormat:@"%@%04d", [child name], next];
        check = [d objectForKey:key];
        while( check != nil ) {
            next++;
            key = [NSString stringWithFormat:@"%@%04d", [child name], next];
            check = [d objectForKey:key];
        }
        [d setObject:v forKey:key];  
    }
    else
    {
          [d setObject:v forKey:[child name]];
    }
于 2012-08-17T01:13:50.760 回答
0

我不确定你是如何应用你的修复的,你是替换 deserializeAsDictionary 中的整个代码还是附加到代码的末尾?

看到代码行有一个 for 循环

  [d setObject:v forKey:[child name]];

找到了,所以我猜你只是扩展了它,而不是关闭 for 循环,你只是在这里扩展它,对吗?

于 2012-04-27T22:04:04.020 回答