如何parsing与其关键string没有引号的JSON?
我想parsing从iOS的SBJSON框架中的以下url产生的json输出http://maps.google.com/maps?q=school&mrt=yp&sll=13.006389,80.2575&output=json
while(1);{title:"school - Google Maps",url:"/maps?q=school\x26mrt=yp\x26sll=13.006389,80.2575\x26ie=UTF8\x26hq=school\x26hnear=",urlViewport:false,ei:"RCu3T4eeMqSiiAe7k-yZDQ",form:{selected:"q",q:{q:"school",mrt:"yp",what:"school",near:""},d:{saddr:"",daddr:"",dfaddr:""},geocode:""},
我正在使用http://www.bodurov.com/JsonFormatter/在线阅读。
在ASIHttpRequest响应方法中我删除while(1); 从响应
NSString *responseString = [[request resonseString]substringFromIndex:9]; //to remove while(1) SBJSONParser * parser = [[SBJSONParser alloc]init]; NSDictionary *jsonDict = (NSDictionary*)[parser objectFromString:responseString]; NSLog(@"%@",jsonDict) // prints null // [responseString JSONValue] also reports error
我猜JSON键没有双引号引起的问题。
我们得到{ title: “hospital – Google Maps”,“urlViewport”:false}而不是{ “title”: “hospital – Google Maps”,“urlViewport”:false,
请帮我parsing一下从Google返回的这个复杂的JSON结构。
这对我的情况更好,因为我的值包含导致上述答案中的正则expression式不正确匹配的时间。
json = [json stringByReplacingOccurrencesOfString: @"(\\w*[A-Za-z]\\w*)\\s*:" withString: @"\"$1\":" options: NSRegularExpressionSearch range: NSMakeRange(0, json.length)];
你需要添加缺less的引号到键,所以试试这个:
responseString = [responseString stringByReplacingOccurrencesOfString:@"(\\w+)\\s*:" withString:@"\"$1\":" options:NSRegularExpressionSearch range:NSMakeRange(0, [responseString length])];
这应该适用于给定的JSONstring。