如何将JSON字符串转换为NSArray? [英] How to convert a JSON String into an NSArray?
本文介绍了如何将JSON字符串转换为NSArray?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
将NSString转换为NSArray时遇到问题.
I am facing an issue while converting an NSString to NSArray.
我的字符串是:
["Default", "Discipleship", "Faith", "Family", "Hope",
"Life Building", "Love", "Missions", "Relationships"]
我想做的是从此字符串中获取元素(默认,学科等),并将其放入NSArray中.
What i want to do is get the elements(Default,Discipleship etc.) out of this string and put them into an NSArray.
我已经尝试了很多,但是无法完成,请帮助 任何帮助都会很棒,在此先感谢
I have tried a lot but couldn't get it done, please help Any help would be great , thanks in advance
推荐答案
首先,您将字符串转换为NSData:
First you convert your string to NSData:
NSString* str = @"[\"Default\",\"Discipleship\",\"Faith\",\"Family\",\"Hope\",\"Life Building\",\"Love\",\"Missions\",\"Relationships\"]";
NSData* data = [str dataUsingEncoding:NSUTF8StringEncoding];
然后,您使用:
NSError *e;
NSArray *array = [NSJSONSerialization JSONObjectWithData:data options:nil error:&e];
对象数组包含JSON文本的元素.
The object array contains the elements of the JSON text.
这篇关于如何将JSON字符串转换为NSArray?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文