1

我有一个使用 RestKit 和 CoreData 的 iOS 应用程序。我在发出 POST 请求时遇到了障碍,因为请求正文是由NSDictionary. 我遇到的问题是请求正文需要有重复的键。 NSDictionary需要唯一的键,所以我不知道如何使这项工作。

这是服务器期望请求正文的方式。

<node>
    <personId>2</personId>
    <status>2</status>
    <title>Dinosaur unearthed somewhere out there. </title>

    <point>
        <city>Somewhere</city>
        <copy><![CDATA[An amazing discovery was unearthed as a local farmer was plowing his field]]></copy>
        <state>Outthere</state>
        <sequence>1</sequence>
    </point>

    <point>
        <city>Somwhere</city>
        <copy><![CDATA[Archeologists from around the world are amazed at the pristine condition of the remains. Also of note is that the farmer was only using a single blade plow on his John Deere tractor when it was unearthed. ]]></copy>
        <sequence>2</sequence>
        <state>Outthere</state>
    </point>

    <point>
        ......
    </point>
</node>

这是我尝试使其工作的简化版本....

params = [[NSMutableDictionary alloc] init];
nodes = [[NSMutableDictionary alloc] init];
nodeParams = [[NSMutableDictionary alloc] init];

NSString *personId = @"2";
NSString *status = @"2";
NSString *title = @"Dinosaur unearthed somewhere out there.";
NSString *city = @"Somewhere";
NSString *state = @"OutThere";
NSString *copyField = @"Testing this out to see if it works";

//Here I set up the point layer of the request body
//In my code this three line section is in a loop. Obviously this does not work because it just overwrites the objectForKey each time through the loop.
[params setObject:copyField forKey:@"copy"];
[params setObject:city forKey:@"city"];
[params setObject:state forKey:@"state"];

//Here I Set up the Node Layer of the request body
[nodeParams setObject:params forKey:@"point"];
[nodeParams setObject:personId forKey:@"personId"];
[nodeParams setObject:status forKey:@"status"];
[nodeParams setObject:title forKey:@"title"];
[nodes setObject:nodeParams forKey:@"node"];

NSLog(@"The Dictionary is %@",nodes);

在运行时,日志显示主体的格式正确,除了它们只是一个点层,并且它填充了循环中最后一次传递的数据。有谁知道解决这个问题的任何技巧?

作为注释,我认为该postObject方法需要 NSDictionary,因为它将字典传递给 JSON 序列化工具。我怀疑序列化工具期望传入字典。如果有人知道,如果我错了,请纠正我。

4

1 回答 1

0

好吧,我终于想通了。直到我将上面的 XML 转换为 JSON,我才意识到它是如何完成的。关键是识别 Array 和 NSDictionary 在 'NSJSONSerialization' 类中的格式。

这是服务器如何期待 JSON 的示例。

{ "status":"2",
  "title":"test #5",
  "personId":"1",
  "point":[
           { "copy":"<p class=\"pt\" data-seq=\"1\">This is paragraph #1<\/p>",
             "state":"OutThere",
             "city":"Somewhere",
             "sequence":"1"
           },

           { "copy":"<p class=\"pt\" data-seq=\"2\">This is paragraph #2<\/p>",
             "state":"OutThere",
             "city":"Somewhere",
             "sequence":"2"
           }
      ]
}

这里要注意的关键是该"point"字段是和字典数组。[表示一个数组。一旦我最终了解这一点,我意识到每次通过循环我都可以重新初始化我的字典,添加新对象,然后将当前字典添加到数组中,如下所示。

while (![theScanner isAtEnd]) {
    count = count +1;

    //Initialize the dictionary
    points = [[NSMutableDictionary alloc] init];

    NSString *htmlTag = [NSString stringWithFormat:@"<p class=\"pt\" data-seq=\"%d\">",count];
    [theScanner scanUpToCharactersFromSet:[NSCharacterSet newlineCharacterSet] intoString:&temp];

    NSString *preTagText = [htmlTag stringByAppendingString:temp];
    NSString *postTagText = [preTagText stringByAppendingString:@"</p>"];
    NSString *sequence = [NSString stringWithFormat:@"%d",count];
    [points setObject:postTagText forKey:@"copy"];
    [points setObject:sequence forKey:@"sequence"];
    [points setObject:city forKey:@"city"];
    [points setObject:state forKey:@"state"];
    [pointArray addObject:points];
     points = nil;

}
[params setObject:titleText forKey:@"title"];
[params setObject:personIdNumber forKey:@"personId"];
[params setObject:status forKey:@"status"];
[params setObject:pointArray forKey:@"point"];

循环完成后,我只需将其pointArray作为带有键的对象添加到字典中point(感谢 Wain)。我将titlestatuspersonId对象添加到同一个字典中,并将其用作 Restkit POST 请求所需的 NSDictionary。

于 2013-10-31T01:23:00.347 回答