开发者

JSON for Objective-C messes up my array

开发者 https://www.devze.com 2023-03-25 16:57 出处:网络
This is a strange error and I\'m not sure if I\'m using JSON correctly. When I get a response from the server with a proper array, JSON chews it up, turning most of the values to zero and I think it m

This is a strange error and I'm not sure if I'm using JSON correctly. When I get a response from the server with a proper array, JSON chews it up, turning most of the values to zero and I think it might've forgotten by keys also.

2011-08-01 16:08:15.981 My Alerts[2746:b303] From Server: {"cycleStart":"May 1, 2011","cycleEnds":"May 29, 2011","avg_usage":0,"estimate":0,"totalBudget":0,"Usage":0,"Cost":0}
2011-08-01 16:08:15.982 My Alerts[2746:b303] After JSON: (
   0,
   0,
   0,
   0,
   "May 29, 2011",
   "May 1, 2011",
   0
)

Here is my code showing where JSON is being used.

NSString *post = [NSString stringWithFormat:@"username=%@&acct=%@", username, account];
NSLog(@"POST: %@", post);
NSData *postData = [NSData dataWithBytes: 开发者_如何学C[post UTF8String] length: [post length]];

// Submit login data
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString: @"http://***.****.***/file1.php"]];
[request setHTTPMethod: @"POST"];
[request setValue: @"application/x-www-form-urlencoded" forHTTPHeaderField: @"Content-Type"];
[request setHTTPBody: postData];

// Retreive server response
NSURLResponse *response;
NSError *err;
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];
if (returnData == nil) {
    NSLog(@"ERROR: %@", err);
}

NSString *content = [NSString stringWithUTF8String:[returnData bytes]];
NSLog(@"From Server: %@", content); // <---JSON string returned perfectly from server

if (content != @"FAIL")
{
    NSArray *viewDetail = [[NSArray alloc] init];
    viewDetail = [[[content JSONValue] allValues] mutableCopy]; // <--JSON appears to be chewing it up
    NSLog(@"After JSON: %@", viewDetail); // <--Array is now messed up here.

    // Do stuff with viewDetail array

    [viewDetail release];
}


I can't see what's wrong. You wanted allValues, you got all the values. Aside from the ordering, the "from server" and "after JSON" are equivalent.

The ordering is not there because NSDictionary (which your JSONValue returns, I believe) does not guarantee order. And the keys are not there because you did not request keys, you requested the values.

If you wanted keys, there's allKeys method. And if you wanted a specific value, there's objectForKey: or valueForKey:. You can also use various enumerators. For instance, I believe this should work:

NSDictionary *dict = [content JSONValue];
for (id key in dict) {
    NSLog(@"key: %@   value:%@", key, [dict objectForKey:key]);
}


JSON isn't chewing anything up - most of your values are, in fact, 0. You're not working with plain arrays, you're working with key-value pairs. In Objective-C these become an NSDictionary.

0

精彩评论

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

关注公众号