如何解析不带引号的 KEY 字符串的 JSON?

2024-02-01

我想解析 iOS SBJSON 框架中以下 url 产生的 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/ http://www.bodurov.com/JsonFormatter/在线阅读。

在 ASIHttpRequest 响应方法中我删除了同时(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":“医院 - 谷歌地图”, “urlViewport”:假, },我们得到{title:“医院 - 谷歌地图”, “urlViewport”:假 }

请帮我解析这个从 Google 返回的复杂 JSON 结构。


您需要将缺少的引号添加到键中,因此请尝试以下操作:

responseString = [responseString stringByReplacingOccurrencesOfString:@"(\\w+)\\s*:" 
                                 withString:@"\"$1\":" 
                                 options:NSRegularExpressionSearch 
                                 range:NSMakeRange(0, [responseString length])];

这应该适用于给定的 JSON 字符串。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何解析不带引号的 KEY 字符串的 JSON? 的相关文章

随机推荐