1

SBJson ライブラリを自由に使用できますが、現在、iOS で NSJSONSerialization クラスのみを使用しています。に電話をかけています

http://www.google.com/dictionary/json?callback=dict_api.callbacks.id100&q=test&sl=en&tl=en&restrict=pr%2Cde&client=te

そして、パラメーターを含む次の Json ファイルを返します。

dict_api.callbacks.id100({...}, 200, null)

私が言えることは、{..} の外側にある無関係なものが私を台無しにしていることです。Objective C を使用して、すべてを削除して {...} だけを残すにはどうすればよいですか? そうすれば、NSDictionary に直接アクセスできます。それが重要な場合は、データを NSData オブジェクトに格納しています。今日Jsonを使い始めたばかりなので、助けていただければ幸いです。

NSDictionary *allDataDictionary = [NSJSONSerialization JSONObjectWithData:self.webData options:0 error:nil];

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    [self.webData setLength:0];
    NSLog(@"1");

}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    NSLog(@"Failed with error");
    NSLog(@"2");

}

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [self.webData appendData:data];
    NSLog(@"3");

}
4

1 に答える 1

2

これはハックですが、次のようにすることもできます (最初の '{' と最後の '}' を見つけてください)。

// Decode the web response data into a string, then:
NSRange begin = [someString rangeOfString:@"{" options:NSLiteralSearch];
NSRange end = [someString rangeOfString:@"}" options:NSBackwardsSearch|NSLiteralSearch];
// Add error checking!
NSString *jsonPart = [someString substringWithRange:NSMakeRange(begin.location, (end.location - begin.location) + 1)];

編集 - より良いハック

JSON はオブジェクトではない可能性があるため、JSONP の括弧を取得するだけです。

NSRange begin = [responseStringJSONPart rangeOfString:@"(" options:NSLiteralSearch];
NSRange end = [responseStringJSONPart rangeOfString:@")" options:NSBackwardsSearch|NSLiteralSearch];
parseFail = (begin.location == NSNotFound || end.location == NSNotFound || end.location - begin.location < 2);
if (!parseFail)
{
    responseStringJSONPart = [responseStringJSONPart substringWithRange:NSMakeRange(begin.location + 1, (end.location - begin.location) - 1)];
}
于 2013-06-16T06:32:25.250 に答える