开发者

What should the return value of a JSON GET or POST look like?

开发者 https://www.devze.com 2023-03-14 13:48 出处:网络
I\'m new to JSON and just starting to wrap my head around it\'s functionality. I\'m trying to see if I can get print some data from some JSON methods. I\'ve been alternating between the first one an

I'm new to JSON and just starting to wrap my head around it's functionality.

I'm trying to see if I can get print some data from some JSON methods. I've been alternating between the first one and the one that is commented out. The ideas is to see if I can get anything printing:

id newConnection =  [scAPI performMethod:@"GET" onResource:@"me/connections.json" withParameters:nil context:nil userInfo:nil];



//  id newConnection =   [scAPI performMethod:@"POST"
//            onResource:@"connections"
//        withParameters:[NSDictionary dictionaryWithObjectsAndKeys:
//                        @"facebook_profile", @"service",
//                        @"imc://connection", @"redirect_uri",
//                        @"touch", @"display", //optional, forces services to use the mobile auth page if available
//                        nil]
//               context:nil
//              userInfo:nil];

   NSLog(@"newConnection %@", newConnection);


   NSLog(@"Is of type: %@", [newConnection class]);



   NSDictionary *dict = [newConnection objectFromJSONString];


for (id key in dict) {

    NSLog(@"key: %@, value: %@", key, [dict objectForKey:key]);

}

The above code doesn't err and I get logs such as:

What should the return value of a JSON GET or POST look like?

Does this look right? How do 开发者_如何转开发I properly use these JSON methods to get a dictionary of values?


EDIT 1

To be clear I'm using JSONKit :)


I would personally recommend using the SBJSON library. Getting a dictionary with it is straightforward.

Get a response back from a connection, and then use the following code (where response is an NSString containing the response from the server):

SBJsonParser *parser = [[SBJsonParser alloc] init];
NSArray *returnData = [parser objectWithString:[response stringByReplacingOccurrencesOfString:@"\\\\" withString:@"\\"]];
[parser release];

NSDictionary *returnDict = (NSDictionary *)returnData;

This is unrelated, but for examining JSON data, I would also recommend Online JSON Viewer. You can paste in your JSON strings and view it with a collapsable array structure. Very convenient.

0

精彩评论

暂无评论...
验证码 换一张
取 消