开发者

Parsing JSON string in Objective C

开发者 https://www.devze.com 2023-04-11 04:53 出处:网络
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.

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:"",f开发者_JAVA百科eature_id:"0x479a721ec2b1be6b:0x75e85d6b8e91e55b",type:33}]}}],target_type:53522,confidence:0.0060176870408534602},


A regular expression that would pluck out that city name is:

/query:"([^"]+)"/m

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.


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:

NSString *myJSONStr = ... //Get string
NSArray *myJSONDataObj = [myJSONStr objectFromJSONString];

NSString *cityName = [[myJSONDataObj objectAtIndex:1] objectForKey:@"query"];


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.


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},

{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},

]}

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号