简体   繁体   中英

parse json from post request ios afnetworking

I'm working on an iOS app (pretty new to iOS) and I have my registration flow working where i post data to my server... The server returns JSON, but my question is, how do I parse that into variables?

The data that it returns is:

 {"token":"67f41f60-9a9a-11e3-8310-11b6baf18c40","userId":13,"stationId":1}

The code I'm using to make the post request is:

 [manager POST:@"http://localhost:3000/service/register_user/" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"JSON: %@", responseObject);


} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error!!!!!: %@", error);
}];

Also, when I make the post request, this is what I get in the log in xcode

 2014-02-20 20:50:39.799 FlashoverResponse[4668:70b] Error!!!!!: Error Domain=AFNetworkingErrorDomain Code=-1016 "Request failed: unacceptable content-type: text/html" UserInfo=0x8a5e4f0 {NSErrorFailingURLKey=http://localhost:3000/service/register_user/, AFNetworkingOperationFailingURLResponseErrorKey=<NSHTTPURLResponse: 0x8a5e200> { URL: http://localhost:3000/service/register_user/ } { status code: 200, headers {
Connection = "keep-alive";
"Content-Length" = 74;
"Content-Type" = "text/html; charset=utf-8";
Date = "Fri, 21 Feb 2014 01:50:39 GMT";
"X-Powered-By" = Express;

} }, NSLocalizedDescription=Request failed: unacceptable content-type: text/html}

Is your server configured to respond in text/json application/json ?

and/or have you set this?

manager.responseSerializer.acceptableContentTypes

it takes a NSSet of content types this parser will attempt to parse, if you cannot change the server to respond in json you can add text/html into that set, so the parser will parse it anyway.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM