次のような AFNetworking ツールを使用して Web サービスを利用しています。
NSURLRequest *request = [NSURLRequest requestWithURL:url];
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
[self GotJSONSuccess:JSON :response ];
} failure: nil ];
[operation start];
Web サービスが応答し、次の json が返されます。
[
{
"statusid": 1,
"statusdesc": "ASSIGNED"
},
{
"statusid": 2,
"statusdesc": "COMPLETED"
},
{
"statusid": 3,
"statusdesc": "IN TRANSIT"
},
{
"statusid": 4,
"statusdesc": "DELAYED"
},
{
"statusid": 5,
"statusdesc": "ON HOLD"
}
]
以下を使用して、json を解析しようとしています。
- (void)GotJSONSuccess: (NSString*) JSON : (NSHTTPURLResponse*) response
{
NSString *newString = [NSString stringWithFormat:@"%@",JSON];
NSLog(@"response: %@", JSON);
NSData* data = [newString dataUsingEncoding:NSUTF8StringEncoding];
NSError* error;
id jsonObjects = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
if (error){
NSLog(@"error is %@", [error localizedDescription]);
return;
}
NSArray *keys = [jsonObjects allKeys];
for (NSString *key in keys){
NSLog(@"%@ is %@",key, [jsonObjects objectForKey:key]);
}
}
ただし、コードはエラー ブロックに分類され、出力は「操作を完了できませんでした。(Cocoa エラー 3840.)」です。
この単純なjsonの解析で間違っているのは何ですか?
私が取っているものよりも優れた解析アプローチはありますか?
可能であれば、ネイティブの iOS クラスと解析メソッドに固執したいと思います。