将 JSON 从 ASIHTTPRequest 传递到 django
我已经用尽了其他线程,所以我在这里发布这个问题。请原谅我一路上所犯的任何新手错误。我读了很多书,我觉得我很困惑。
目标: 我正在尝试将数据从 Objective-C 中的表单传递到我的 Django Web 服务。为了帮助解决这个问题,我使用了 ASIHTTPRequest
类来促进信息传输。一旦发送到网络服务,我想将该数据保存到我的 sqlite3 数据库中。
程序: 在 Objective-C 方面: 我已将输入的表单数据及其各自的键存储在 NSDictionary 中,如下所示:
NSDictionary *personInfo = [NSDictionary dictionaryWithObjectsAndKeys:firstName.text, @"fName", middleName.text, @"mName", lastName.text, @"lName", nil];
我已使用委托将其添加到不同类中的 ASIHTTPRequest 中。为了简单起见,我在下面的代码块中使 NSDictionary 与上面相同,如下所示:
NSString *jsonPerson = [personInfo JSONRepresentation];
[request addRequestHeader: @"Content-Type" value:@"application/json; charset=utf-8"];
[request appendPostData:[jsonPerson dataUsingEncoding:NSUTF8StringEncoding]];
[request setRequestMethod:@"POST"];
[request startAsynchronous];
NSLog 显示我传递的字符串,如下所示,至少在 JSONLint
{"mName":"Arthur","lName":"Smith","fName":"Bob"}
因为我看到来自 ASIHTTPRequest 的有效 JSON,并且操作是从 requestfinished: 而不是 requestfailed: 运行的,所以我做出假设那问题很可能不在 Objective-C 方面,而是在 django 方面。
这是我到目前为止所尝试过的:
json.loads(request.POST)
>>expected string or buffer
json.loads('request.POST')
>>no JSON object to decode
json.loads(request.raw_post_data)
>>mNamelNamefName
incoming = request.POST
>>{"mName":"Arthur","lName":"Smith","fName":"Bob"}
incoming = request.POST
onlyValues = incoming.iterlists()
>>(u'{"mName":"Arthur","lName":"Smith","fName":"Bob"}', [u''])
......以及其他一些看似牵强的变化。我已经记录了日志,可以详细说明。我能找到的唯一希望是在最后一个例子中;看起来它将整个字符串视为键,而不是像我预期的那样分解每个 dict 对象和键。
我意识到这是非常基本的,我通常不会问,但这个问题让我特别困惑。我还记得在某处读过,python 无法识别每个对象和键周围的双引号,为了使其成为 django 喜欢的东西,每个对象和键都应该用单引号引起来。我只是不知道如何让他们那样。
谢谢!
I've exhausted other threads, so I'm posting this question here. Please pardon any newbie mistakes I've made along the way. I've been reading a lot, and I think I'm getting confused.
The Goal:
I'm trying to pass data from a form in objective-c to my django web service. In an effort to assist with this, I've employed the ASIHTTPRequest
class to facilitate information transfer. Once sent to the web service, I'd like to save that data to my sqlite3 database.
Procedure:
On the Objective-C side:
I've stored the inputted form data and their respective keys in an NSDictionary, like this:
NSDictionary *personInfo = [NSDictionary dictionaryWithObjectsAndKeys:firstName.text, @"fName", middleName.text, @"mName", lastName.text, @"lName", nil];
I've added it to my ASIHTTPRequest in a different class by using a delegate. I've made the NSDictionary the same as above in the code block below for simplicity, like so:
NSString *jsonPerson = [personInfo JSONRepresentation];
[request addRequestHeader: @"Content-Type" value:@"application/json; charset=utf-8"];
[request appendPostData:[jsonPerson dataUsingEncoding:NSUTF8StringEncoding]];
[request setRequestMethod:@"POST"];
[request startAsynchronous];
And a NSLog shows the string I'm passing to look like this, which validates at least in JSONLint
{"mName":"Arthur","lName":"Smith","fName":"Bob"}
Because I'm seeing what appears to be valid JSON coming from my ASIHTTPRequest, and actions are running from requestfinished: rather than requestfailed:, I'm making the assumption that the problem more than likely isn't on the Objective-C side, but rather on the django side.
Here's what I've tried so far:
json.loads(request.POST)
>>expected string or buffer
json.loads('request.POST')
>>no JSON object to decode
json.loads(request.raw_post_data)
>>mNamelNamefName
incoming = request.POST
>>{"mName":"Arthur","lName":"Smith","fName":"Bob"}
incoming = request.POST
onlyValues = incoming.iterlists()
>>(u'{"mName":"Arthur","lName":"Smith","fName":"Bob"}', [u''])
...and a smattering of other seemingly far-fetched variations. I've kept a log, and can elaborate. The only hope I've been able to find is in the last example; it looks like it's treating the entire string as the key, rather than breaking up each dict object and key as I would have expected.
I realize this is terribly elementary and I don't normally ask, but this problem has me particularly stumped. I do also remember reading somewhere that python won't recognize the double-quotes around each object and key, that to get it to something django likes, each should be surrounded by single-quotes. I just don't have any idea how to get them that way.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这可能有点麻烦,但您可以在
objective c
中尝试一些简单的正则表达式,看看是否确实如此。可能会出现一些错误,因为我没有运行代码。
This might be a little cumbersome but you may try some simple regexp in
objective c
just to see if that is really the caseThere might be some errors because I didn't run the code.