在 Objective C 中解析 JSON 字符串
我有一个非常非常长的 NSString,其中出现了 {query:"City Name"
城市名称可以是任意长度。我希望能够将所有城市名称提取到一个数组中。
我该怎么做?
{建议:[{查询:“钦奈、泰米尔纳德邦、印度",解释:{term:[{start:0,end:7,feature_type:"",feature_id:"0x3a5265ea4f7d3361:0x6e61a70b6863d433",matched:1,target:1,term_sta rt:0,term_end:7,类型:37},{开始:9,结束:19,feature_type:"",feature_id:"0x3b00c582b1189633:0x559475cc463361f0",类型:545},{开始:21,结束:26,fea ture_type:"",feature_id:"0x30635ff06b92b791:0xd78c4fa1854213a6",type:33}]},操作:2,详细信息:[{value:"IN",解释:{term:[{start:0 ,end:2,feature_type:"",feature_id:"0x30635ff06b92b791:0xd78c4fa1854213a6",type:33}]}}],target_type:37,confidence:0.54653339144809898},
{查询:“钦奈”,解释:{term:[{start:0,end:7,feature_type:“”,feature_id:“0x1000000000000000:0x42a933fdc808d659”,匹配:1,目标:1,term_start:0,term_end:7,类型:856338}]},操作:2,target_type:856338,置信度:0.019136800753524912},{查询:“钦奈,第一大道, 纽约, NY, 联合状态”,解释:{term:[{start:0,end:7,feature_type:“”,feature_id:“0x89c258ba6002c507:0x73911ed16842e4ba”,匹配:1,目标:1,term_start:0,term_end:7,类型:53522 },{开始:9,结束:19,fea ture_type:“”,feature_id:“0x89c258c29e17b909:0x2bf768350d30b89e”,类型:17},{开始:21,结束:29,fea ture_type:“”,feature_id:“0x89c24fa5d33f083b:0xc80b8f06e177fe62”,类型:37},{开始:31,结束:33,壮举ure_type:“”,feature_id:“0x4ccc4bf0f123a5a9:0xddcfc6c1de189567”,类型:545},{开始:35,结束:48,fea ture_type:“”,feature_id:“0x54eab584e432360b:0x1c3bb99243deb742”,类型:33}]},操作:2,详细信息s:[{value:"US",解释:{term:[{start:0,end:2,feature_type:"",feature_id:"0x54eab584e43 2360b:0x1c3bb99243deb742",类型:33}]}}],target_type:53522,置信度:0.010777814592616199},
{query:“钦奈绝缘,钦奈,泰米尔纳德邦,印度”,解释:{term:[{start:0,end:18,feature_type:"",feature_id:"0x3a52675249167ca9:0xaf9a262855cfd248",matched:1,target:1,term_start:0,term_end:7,type:535 22},{开始:20,结束:27,feature_type:"",feature_id:"0x3a5265ea4f7d3361:0x6e61a70b6863 d433",类型:37},{开始:29,结束:39,feature_type:"",feature_id:"0x3b00c582b1189633:0x5 59475cc463361f0",类型:545},{开始:41,结束:46,feature_type:"",feature_id:"0x30635ff06b92b791:0xd78c4fa1854213a6",类型:33}]},操作:2,详细信息:[{值:"IN" ,解释ation:{term:[{start:0,end:2,feature_type:"",feature_id:"0x30635ff06b92b791:0xd78c4fa1854213a6",type:33}]}}],target_type:53522,confidence:0.0078605968414456173},
{查询:“钦奈,Knobelsdorffstraße,德国”,解释:{term:[{start:0,end:7,feature_type:"",feature_id:"0x47a851282ee06d3b:0xd89e4a3bcf4999a3",matched:1,target:1,term_sta rt:0,term_end:7,类型:53522},{开始:9,结束:27,feature_type:"",feature_id:"0x47a850d5ff0b26d9:0xb882dad42f6743db",类型:17},{开始:29,结束:36,fea ture_type:"",feature_id:"0x479a721ec2b1be6b:0x75e85d6b8e91e55b",type:33}]},操作:2,详细信息:[{value:"DE",解释:{term:[{start:0,e nd:2,feature_type:"",feature_id:"0x479a721ec2b1be6b:0x75e85d6b8e91e55b",type:33}]}}],target_type:53522,confidence:0.0060176870408534602},
I have a very very long NSString which has occurrences of {query:"City Name"
The City Name can be of any length. I want to be able to extract all the City Name into an array.
How can I do this?
{suggestion:[{query:"Chennai, Tamil Nadu, India",interpretation:{term:[{start:0,end:7,feature_type:"",feature_id:"0x3a5265ea4f7d3361:0x6e61a70b6863d433",matched:1,target:1,term_start:0,term_end:7,type:37},{start:9,end:19,feature_type:"",feature_id:"0x3b00c582b1189633:0x559475cc463361f0",type:545},{start:21,end:26,feature_type:"",feature_id:"0x30635ff06b92b791:0xd78c4fa1854213a6",type:33}]},operation:2,details:[{value:"IN",interpretation:{term:[{start:0,end:2,feature_type:"",feature_id:"0x30635ff06b92b791:0xd78c4fa1854213a6",type:33}]}}],target_type:37,confidence:0.54653339144809898},
{query:"Chennai",interpretation:{term:[{start:0,end:7,feature_type:"",feature_id:"0x1000000000000000:0x42a933fdc808d659",matched:1,target:1,term_start:0,term_end:7,type:856338}]},operation:2,target_type:856338,confidence:0.019136800753524912},{query:"Chennai, 1st Avenue, New York, NY, United States",interpretation:{term:[{start:0,end:7,feature_type:"",feature_id:"0x89c258ba6002c507:0x73911ed16842e4ba",matched:1,target:1,term_start:0,term_end:7,type:53522},{start:9,end:19,feature_type:"",feature_id:"0x89c258c29e17b909:0x2bf768350d30b89e",type:17},{start:21,end:29,feature_type:"",feature_id:"0x89c24fa5d33f083b:0xc80b8f06e177fe62",type:37},{start:31,end:33,feature_type:"",feature_id:"0x4ccc4bf0f123a5a9:0xddcfc6c1de189567",type:545},{start:35,end:48,feature_type:"",feature_id:"0x54eab584e432360b:0x1c3bb99243deb742",type:33}]},operation:2,details:[{value:"US",interpretation:{term:[{start:0,end:2,feature_type:"",feature_id:"0x54eab584e432360b:0x1c3bb99243deb742",type:33}]}}],target_type:53522,confidence:0.010777814592616199},
{query:"Chennai Insulation, Chennai, Tamil Nadu, India",interpretation:{term:[{start:0,end:18,feature_type:"",feature_id:"0x3a52675249167ca9:0xaf9a262855cfd248",matched:1,target:1,term_start:0,term_end:7,type:53522},{start:20,end:27,feature_type:"",feature_id:"0x3a5265ea4f7d3361:0x6e61a70b6863d433",type:37},{start:29,end:39,feature_type:"",feature_id:"0x3b00c582b1189633:0x559475cc463361f0",type:545},{start:41,end:46,feature_type:"",feature_id:"0x30635ff06b92b791:0xd78c4fa1854213a6",type:33}]},operation:2,details:[{value:"IN",interpretation:{term:[{start:0,end:2,feature_type:"",feature_id:"0x30635ff06b92b791:0xd78c4fa1854213a6",type:33}]}}],target_type:53522,confidence:0.0078605968414456173},
{query:"Chennai, Knobelsdorffstraße, Germany",interpretation:{term:[{start:0,end:7,feature_type:"",feature_id:"0x47a851282ee06d3b:0xd89e4a3bcf4999a3",matched:1,target:1,term_start:0,term_end:7,type:53522},{start:9,end:27,feature_type:"",feature_id:"0x47a850d5ff0b26d9:0xb882dad42f6743db",type:17},{start:29,end:36,feature_type:"",feature_id:"0x479a721ec2b1be6b:0x75e85d6b8e91e55b",type:33}]},operation:2,details:[{value:"DE",interpretation:{term:[{start:0,end:2,feature_type:"",feature_id:"0x479a721ec2b1be6b:0x75e85d6b8e91e55b",type:33}]}}],target_type:53522,confidence:0.0060176870408534602},
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
提取该城市名称的正则表达式是:
也就是说,更好的答案是从 Google 获取有效的 JSON(相当确信他们可以提供),通过 JSON 解析器运行它,然后步行由此产生的对象。
A regular expression that would pluck out that city name is:
That said, the better answer is to get valid JSON from Google (pretty sure they can provide that), run it through a JSON parser, and walk the resulting object.
使用 JSONKit。
这将允许您以 NSArray 或 NSDictionary 对象的形式访问 JSON 字符串的内容。
然后你的代码将如下所示:
Use JSONKit.
This will allow you to access the the contents of the JSON string as a NSArray or NSDictionary object.
Then you code will look something like this:
它基本上是 JSON,但语法无效,因为属性名称没有用引号括起来(而且数据已被截断,但我假设您知道这一点)。 (此外,“feature_id”值已被混淆,但这似乎并没有让解析器感到不安。)
尽管属性名称存在问题,但某些解析器可能会接受它,或者这将是一个相当简单的过程将引号“过滤”到字符串中。
It's basically JSON, but invalid syntax, since the property names aren't wrapped in quotes (and also the data's been truncated, but I assume you knew that). (Also, the "feature_id" values have been garbled, but that doesn't seem to upset the parser.)
Some parsers may accept it anyway, in spite of the problem with the property names, or it would be a fairly simple process to "filter" the quotes into the string.
如果这是您的完整 json,请在最后添加“]}”,然后尝试解析。我想这应该对你有帮助。
{建议:[{查询:“钦奈、泰米尔纳德邦、印度”,解释:{term:[{start:0,end:7,feature_type:"",feature_id:"0x3a5265ea4f7d3361:0x6e61a70b6863d433",matched:1,target:1,term_st艺术:0,term_end:7,类型:37},{开始:9,结束:19,feature_type:“”,feature_id:“0x3b00c582b1189633:0x559475cc463361f0”,类型:545},{开始:21,结束:26,f eature_type:"",feature_id:"0x30635ff06b92b791:0xd78c4fa1854213a6",type:33}]},操作:2,详细信息:[{value:"IN",解释:{term:[{star t:0,结束:2,feature_type:“”,feature_id:“0x30635ff06b92b791:0xd78c4fa1854213a6”,类型:33}]}}],target_type:37,置信度:0.54653339144809898},
]}
If this is your complete json then add "]}" in the last and then try to parse. I think this should help you.
{suggestion:[{query:"Chennai, Tamil Nadu, India",interpretation:{term:[{start:0,end:7,feature_type:"",feature_id:"0x3a5265ea4f7d3361:0x6e61a70b6863d433",matched:1,target:1,term_start:0,term_end:7,type:37},{start:9,end:19,feature_type:"",feature_id:"0x3b00c582b1189633:0x559475cc463361f0",type:545},{start:21,end:26,feature_type:"",feature_id:"0x30635ff06b92b791:0xd78c4fa1854213a6",type:33}]},operation:2,details:[{value:"IN",interpretation:{term:[{start:0,end:2,feature_type:"",feature_id:"0x30635ff06b92b791:0xd78c4fa1854213a6",type:33}]}}],target_type:37,confidence:0.54653339144809898},
]}