首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无效touchJSON字符串

无效touchJSON字符串
EN

Stack Overflow用户
提问于 2010-11-14 17:49:52
回答 1查看 1.1K关注 0票数 2

我得到一个无效的JSON字符串,它包含;(字符)。你猜怎么回事?

我的代码:

代码语言:javascript
复制
-(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格式with =;

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2010-11-15 01:46:09

我不能百分之百确定这个问题是什么。您可以执行有效的HTTP连接,它从Google发出有意义的请求(如果删除中间的六个空格,这几乎肯定是代码复制和粘贴到这里的结果)。你把结果累积起来。在给定的代码中,您似乎泄漏了对象jsonData,但我认为这与问题无关。

您使用的CJSONDeserializer对象,我还没有听说过,但似乎在谷歌上经常提到,所以可能是值得信赖的。它返回一个有效的NSDictionary。你把字典打印出来,字典里有正确的结果。

混淆之处在于,当您将字典打印到控制台时,它看起来与您收到的JSON不一样吗?如果是这样的话,那是因为它不再有来自JSON和Cocoa的任何概念,它早于JSON标准,因此不用于日志记录。

无论如何,feedDictionary是一个有效的字典。以下内容如下:

代码语言:javascript
复制
NSLog(@"%@", [feedDictionary objectForKey:@"status"]);

会打印字符串'OK‘。这是:

代码语言:javascript
复制
NSArray *addressComponents = [feedDictionary objectForKey:@"address_components"];
for(NSDictionary *component in addressComponents)
{
    NSLog(@"%@", [component objectForKey:@"long_name"]);
}

会按顺序打印字符串“1600”、“圆形剧场”、“山景城”、“圣何塞”、“圣克拉拉”、“加利福尼亚”、“美国”、“94043”。

如果您想要将原始JSON打印到控制台,您可能需要这样的内容(假设结果返回为UTF8):

代码语言:javascript
复制
-(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);  */
}

尽管如此,您仍然需要将其解析为字典才能得到有意义的结果。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4178772

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档