Convert NSArray into array of JSON objects

青春壹個敷衍的年華 提交于 2019-12-02 02:35:01

问题


I'd like to create a JSON array of objects from the resultsArray.

NSMutableArray *resultsArray = [NSMutableArray array];
FMResultSet *resultsSet = [database executeQuery:queryString];
while ([resultsSet next]) {
    [resultsArray addObject:[resultsSet resultDictionary]];
}

For clarification, I'm using FMDB to query a database. I'm then storing the resulting objects within the resultsArray. From here I want to convert that results array into a JSON array of objects.


回答1:


You can use NSJSONSerialization class to create a JSON file from your dictionary using the class method

NSData * JSONData = [NSJSONSerialization dataWithJSONObject:resultsArray
                                                    options:kNilOptions
                                                      error:&error];

For more info look at apple documentation



来源:https://stackoverflow.com/questions/13961590/convert-nsarray-into-array-of-json-objects

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!