我有一个使用RestKit和CoreData的iOS应用。由于请求正文由NSDictionary
组成,因此我遇到了发出POST请求的路障。我遇到的问题是请求正文需要有重复的密钥。 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序列化工具。我怀疑序列化工具是否希望传入字典。如果有人知道,如果我错了,请更正我。
答案 0 :(得分:0)
好的,我终于弄明白了。直到我将上面的XML转换为JSON,我才意识到它是如何完成的。关键是要识别如何在'NSJSONSerialization'类中格式化Array和NSDictionary。
以下是服务器如何期待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)。我将title
,status
和personId
对象添加到同一个字典中,并将其用作Restkit POST请求所需的NSDictionary。