如何解析不带引号的JSON的KEY字符串? [英] How to parse a JSON having no quotes with its KEY string?

查看:543
本文介绍了如何解析不带引号的JSON的KEY字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在iOS的SBJSON框架中解析以下URL产生的json输出 http://maps.google. com/maps?q = school& mrt = yp& sll = 13.006389,80.2575& output = json

I want to parse the json output resulting from the following url in SBJSON framework for iOS 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/进行在线阅读

I am using http://www.bodurov.com/JsonFormatter/ to read it online.

在ASIHttpRequest响应方法中,我从响应中删除了 while(1);

In ASIHttpRequest response method I removed while(1); from the response

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密钥会引起问题.

I guess JSON key without double quotes is causing problem.

代替{ 标题"::医院-Google地图", "urlViewport":否, },我们得到{ 标题:医院-Google地图", "urlViewport":false }

Instead of { "title": "hospital - Google Maps", "urlViewport": false, }, we get { title: "hospital - Google Maps", "urlViewport": false }

请帮助我解析从Google返回的这种复杂的JSON结构.

Please help me to parse this complex JSON structure returned from Google.

推荐答案

这对我来说效果更好,因为我的值所包含的时间导致上述答案中的正则表达式不正确匹配.

This worked better for my case because my values contained times which caused the regular expression in the above answer to match incorrectly.

json = [json stringByReplacingOccurrencesOfString: @"(\\w*[A-Za-z]\\w*)\\s*:"
                                       withString: @"\"$1\":"
                                          options: NSRegularExpressionSearch
                                            range: NSMakeRange(0, json.length)];

这篇关于如何解析不带引号的JSON的KEY字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆