2

;を含む無効なJSON文字列を取得します。(文字。何が起こっているのか推測できますか?

私のコード:

-(void)getJSONFeed {  
    // Create the URL & Request      
 NSURL *feedURL = [NSURL URLWithString:      
   @"http://maps.googleapis.com/maps/api/geocode/json?      address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&sensor=true"];    
 NSURLRequest *request = [NSURLRequest requestWithURL:feedURL];  
 // Example connection only. Add Timeouts, cachingPolicy in production  
 [NSURLConnection connectionWithRequest:request delegate:self ];  
 // init the jsonData Property  
 jsonData = [[NSMutableData data] retain];  
}  

// NSURLConnection Delegate Methods. You would want to include more for error handling //  
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSMutableData *)data {  
 NSLog(@"Recieving Data...");  
 // Append the incomming data as it is received  
 [jsonData appendData:data];  
 NSLog(@"%@",jsonData);  
}  

-(NSDictionary *)parseJSON:(NSMutableData *)data {  
 NSLog(@"Parsing JSON");       
 NSError *error = nil;  
 NSDictionary *dictionary = [[CJSONDeserializer deserializer]   deserializeAsDictionary:data error:&error];  
 return dictionary;  
}  

// Parse JSON results with TouchJSON. It converts it into a dictionary.  


-(void)connectionDidFinishLoading:(NSURLConnection *)connection {  
 NSLog(@"Fininshed Loading...");  
 NSDictionary * feedDictionary = [self parseJSON:jsonData];  
 NSLog(@"JSON as NSDictionary: %@", feedDictionary);  
}  

{  
    results =     (
                {
            "address_components" =             (
                                {
                    "long_name" = 1600;
                    "short_name" = 1600;
                    types =                     (
                        "street_number"
                    );
                },
                                {
                    "long_name" = "Amphitheatre Pkwy";
                    "short_name" = "Amphitheatre Pkwy";
                    types =                     (
                        route
                    );
                },
                                {
                    "long_name" = "Mountain View";
                    "short_name" = "Mountain View";
                    types =                     (
                        locality,
                        political
                    );
                },
                                {
                    "long_name" = "San Jose";
                    "short_name" = "San Jose";
                    types =                     (
                        "administrative_area_level_3",
                        political
                    );
                },
                                {
                    "long_name" = "Santa Clara";
                    "short_name" = "Santa Clara";
                    types =                     (
                        "administrative_area_level_2",
                        political
                    );
                },
                                {
                    "long_name" = California;
                    "short_name" = CA;
                    types =                     (
                        "administrative_area_level_1",
                        political
                    );
                },
                                {
                    "long_name" = "United States";
                    "short_name" = US;
                    types =                     (
                        country,
                        political
                    );
                },
                                {
                    "long_name" = 94043;
                    "short_name" = 94043;
                    types =                     (
                        "postal_code"
                    );
                }
            );
            "formatted_address" = "1600 Amphitheatre Pkwy, Mountain View, CA 94043, USA";
            geometry =             {
                location =                 {
                    lat = "37.422782";
                    lng = "-122.085099";
                };
                "location_type" = ROOFTOP;
                viewport =                 {
                    northeast =                     {
                        lat = "37.4259296";
                        lng = "-122.0819514";
                    };
                    southwest =                     {
                        lat = "37.4196344";
                        lng = "-122.0882466";
                    };
                };
            };
            types =             (
                "street_address"
            );
        }
    );
    status = OK;
}

アップデート:

どういうわけかそれはプロパティリストとして解釈します。形式は、元のNeXTSTEP形式と似ているようです。

4

1 に答える 1

0

質問が何であるかは100%わかりません。有効なHTTP接続を実行します。これにより、Googleから意味のあるリクエストが行われます(コードのコピーと貼り付けの結果である可能性が高い中央の6つのスペースを削除した場合)。結果を蓄積します。与えられたコードでは、オブジェクトjsonDataをリークしているように見えますが、それは質問とは無関係だと思います。

あなたは私が聞いたことがないがGoogleで一般的に言及されているように見えるCJSONDeserializerオブジェクトを使用しているので、おそらく信頼できます。有効なNSDictionaryを返します。辞書を印刷すると、正しい結果が得られます。

辞書をコンソールに印刷すると、受け取ったJSONと同じように見えないという混乱はありますか?もしそうなら、それはそれがJSONから来たという概念をもはや持っておらず、CocoaはJSON標準よりも前のものであり、したがってそれをロギングに使用しないためです。

いずれにせよ、feedDictionaryは有効な辞書です。以下:

NSLog(@"%@", [feedDictionary objectForKey:@"status"]);

文字列「OK」を出力します。これ:

NSArray *addressComponents = [feedDictionary objectForKey:@"address_components"];
for(NSDictionary *component in addressComponents)
{
    NSLog(@"%@", [component objectForKey:@"long_name"]);
}

文字列「1600」、「Amphitheatre Pkwy」、「Mountain View」、「San Jose」、「Santa Clara」、「California」、「United States」、「94043」の順に印刷されます。

生のJSONをコンソールに出力する場合は、おそらく次のようなものが必要です(結果がUTF8として返されると仮定します)。

-(void)connectionDidFinishLoading:(NSURLConnection *)connection {  
 NSLog(@"Fininshed Loading...");  

 NSString *feedString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
 NSLog(@"JSON was: %@", feedString);
 [feedString release];

 /*NSDictionary * feedDictionary = [self parseJSON:jsonData];  
 NSLog(@"JSON as NSDictionary: %@", feedDictionary);  */
}

それでも、意味のある結果を得るには、辞書に解析する必要があります。

于 2010-11-15T01:46:09.210 に答える