Iphone 在JSON解析中,我在动态解析时遇到了问题
在JSON解析之后,我无法得到确切的输出Iphone 在JSON解析中,我在动态解析时遇到了问题,iphone,Iphone,在JSON解析之后,我无法得到确切的输出 strURLRootView是“RootViewController.m”类的NSString对象 strURLRootView = txt_EnterURL.text; strURLRank = strURLRootView; -(void)viewDidLoad { [self startJSONParsing]; [super viewDidLoad]; } -(void)startJSONParsing {
strURLRootView
是“RootViewController.m”类的NSString
对象
strURLRootView = txt_EnterURL.text;
strURLRank = strURLRootView;
-(void)viewDidLoad
{
[self startJSONParsing];
[super viewDidLoad];
}
-(void)startJSONParsing
{
responseData = [[NSMutableData data] retain];
ArrData = [NSMutableArray array];
NSString *stringWithoutSpaces = [NSString stringWithFormat:@"http://lsapi.seomoz.com/linkscape/url-metrics/%@%2fblog?AccessID=member-10fd5cb877&Expires=1365850483&Signature=m%2F7hZu1y7Sa00NGLFKHYY%2FO3TB4%3D",strURLRank];
strURLRank
也是“CheckRank\u ViewController.m”类的NSString
对象
strURLRootView = txt_EnterURL.text;
strURLRank = strURLRootView;
-(void)viewDidLoad
{
[self startJSONParsing];
[super viewDidLoad];
}
-(void)startJSONParsing
{
responseData = [[NSMutableData data] retain];
ArrData = [NSMutableArray array];
NSString *stringWithoutSpaces = [NSString stringWithFormat:@"http://lsapi.seomoz.com/linkscape/url-metrics/%@%2fblog?AccessID=member-10fd5cb877&Expires=1365850483&Signature=m%2F7hZu1y7Sa00NGLFKHYY%2FO3TB4%3D",strURLRank];
如果我使用
@"http://lsapi.seomoz.com/linkscape/url-metrics/www.yahoo.com%2fblog?AccessID=member-10fd5cb877&Expires=1365850483&Signature=m%2F7hZu1y7Sa00NGLFKHYY%2FO3TB4%3D“
这个URL静态地将给出确切的输出
NSLog(@"stringWithoutSpaces is:--> %@\n\n",stringWithoutSpaces);
NSURLRequest *request1 = [NSURLRequest requestWithURL:
[NSURL URLWithString:stringWithoutSpaces]];
NSLog(@"request1 is:--> %@\n",request1);
[[NSURLConnection alloc] initWithRequest:request1 delegate:self];
}
(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
[responseData setLength:0];
}
(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[responseData appendData:data];
}
(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog(@"Error occured at the time of parsing...");
}
(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
[connection release];
NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
[responseData release];
results = [responseString JSONValue];
lbl_RootDomain.text = [results objectForKey:@"upl"];
lbl_URL.text = [results objectForKey:@"uu"];
lbl_Title.text = [results objectForKey:@"ut"];
lbl_SubDomain.text = [NSString stringWithFormat:@"%f",[[results objectForKey:@"fmrp"] floatValue]];
lbl_MozRank.text = [NSString stringWithFormat:@"%f",[[results objectForKey:@"umrp"] floatValue]];
lbl_DomainAutho.text = [NSString stringWithFormat:@"%d",[[results objectForKey:@"pda"] intValue]];
lbl_XternalLinks.text = [NSString stringWithFormat:@"%d",[[results objectForKey:@"ueid"] intValue]];
lbl_Links.text = [NSString stringWithFormat:@"%d",[[results objectForKey:@"uid"] intValue]];
lbl_PageAutho.text = [NSString stringWithFormat:@"%f",[[results objectForKey:@"upa"] floatValue]];
NSLog(@"results data is:--> %@",results);
NSString *str = [NSString stringWithFormat:@"Root Domain: %@\n URL: %@\n Title: %@\n Subdomain mozRank: %@\n MozRank: %@\n Domain Authority: %@\n External Links: %@\n Links: %@\n Page Authority: %@\n",lbl_RootDomain.text,lbl_URL.text,lbl_Title.text,lbl_SubDomain.text,lbl_MozRank.text,lbl_DomainAutho.text,lbl_XternalLinks.text,lbl_Links.text,lbl_PageAutho.text];
NSLog(@"mail data is:--> \n\n %@",str);
}
我没有得到确切的编码问题。如果我在编码上有错误,那么请告诉我什么是错误的
提前感谢。检查strURLRank是否为空后,使用下面的代码
NSString *stringWithoutSpaces = [NSString stringWithFormat:@"http://lsapi.seomoz.com/linkscape/url-metrics/%@%2fblog?AccessID=member-10fd5cb877&Expires=1365850483&Signature=m%2F7hZu1y7Sa00NGLFKHYY%2FO3TB4%3D",strURLRank];
NSString *urlString=[self trimString:stringWithoutSpaces];
以及使用urlString
下面是在.m文件中定义的方法
-(NSString*) trimString:(NSString *)theString {
NSString *theStringTrimmed = [theString stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]];
return theStringTrimmed;
}
希望这能帮助你。。。。
:)。。。。。。。。。Maulik Trambadiya解决方案
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
[responseData setLength: 0];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[responseData appendData:data];
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
[connection release];
[responseData release];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
responseData = nil;
NSLog(@"%@",responseString);
SBJsonParser *parser = [[[SBJsonParser alloc] init] autorelease];
NSDictionary *responseDict = [parser objectWithString:responseString];
NSLog(@"%@",responseDict);
[responseDict retain];
}
- (NSString*) _formEncodeString: (NSString*) string
{
NSString* encoded = (NSString*) CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,
(CFStringRef) string, NULL, CFSTR("!*'();:@&=+$,/?%#[]"), kCFStringEncodingUTF8);
return [encoded autorelease];
}
- (void)viewWillAppear:(BOOL)animated
{
responseData = [[NSMutableData data] retain];
ArrData = [NSMutableArray array];
NSString *strURLRank=@"www.gmail.com";
NSString *str=@"%2fblog?AccessID=member-10fd5cb877&Expires=1365850483&Signature=";
NSString *str1=@"m%2F7hZu1y7Sa00NGLFKHYY%2FO3TB4%3D";
NSString *stringWithoutSpaces = [NSString stringWithFormat:@"http://lsapi.seomoz.com/linkscape/url-metrics/%@",strURLRank];
NSLog(@"%@",stringWithoutSpaces);
stringWithoutSpaces=[NSString stringWithFormat:@"%@%@%@",stringWithoutSpaces,str,str1];
// stringWithoutSpaces=[stringWithoutSpaces stringByReplacingOccurrencesOfString:@" " withString:@"%20"];
// stringWithoutSpaces = [stringWithoutSpaces stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSLog(@"%@",stringWithoutSpaces);
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:stringWithoutSpaces]];
NSLog(@"%@",request);
[[NSURLConnection alloc] initWithRequest:request delegate:self];
[super viewWillAppear:animated];
}
。。。。。。。。。Maulik Trambadiya解决方案已完成………我认为如果strURLRank返回空值,或者如果此处为空格,则URL不支持空格,则有可能……请检查strURLRank的值……然后请告诉我您在文本字段中输入的哪个值意味着strURLRank的值……包括NSLog以及整个URL NSLog。。。。。。我试图解决您的问题…我想看到您的请求1是:-->“yourURLHere”日志,它在这里打印整个url,而不是www.google.com,使用www.facebook.com,在它没有响应您之后,您的URLRequest中出现了问题,问题是您的字符串带有特殊字符…request1是:-->oh dude我认为您的AccessID和memmberID没有在这个URL中使用,而且您也没有使用这个API的身份验证,所以请遵循此链接并清除您的问题dude…:)这很容易,这不是你的错,但首先注册并获得你的accessID和MemberID…我已经生成并使用了这个accessID和MemberID…然后它也给出了相同的输出。。。。