简体   繁体   English

如何在Objective-C中将NSString对象转换为Array?

[英]How to convert NSString object into Array in Objective-C?

I have below string: 我有下面的字符串:

{"list": {"array":[{"current_rate":20.0,"id":1, "name": "abc"}, 
                   {"current_rate":20.0,"id":2, "name": "xyz"}]}}

I want to convert above string into array like 我想将上面的字符串转换为数组

[current_rate: 20.0, id: 1, name: abc]

I used componentSeperatedByString:@":". 我使用了componentSeperatedByString:@":".

But it gives problem when name field contain ":" string. 但是当名称字段包含“:”字符串时,会出现问题。

Is there any way to convert above string into array. 有什么办法可以将上述字符串转换为数组。

The string you have seems valid JSON. 您拥有的字符串似乎是有效的JSON。 You may want to parse it: 您可能要解析它:

NSData *data = [theString dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:0 error:NULL];

Then you can get the object using the objectForKey: and objectAtIndex: methods on the appropriate classes. 然后,可以在适当的类上使用objectForKey:objectAtIndex:方法获取对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM