objective-c - nsrange.location 不完全是麻烦 makerange 子串 ios objective c

标签 objective-c ios nsstring substring nsrange

嗨,我有这个字符串 { "data": [ { "name": "Lorena Trujillo", "id": "500144123"}, { "name": "George Arcila", "id": "520311359"}, { "name": "Laura Victoria Mu\u00f1oz Rincon", "id": "528543677"}, { "name": "Camilo Andres Santacoloma Mejia", "id": "529547832"}],"paging":{“下一个”:“https://graph.facebook.com/537223119/friends?access_token=AAAAAAITEghMBAI7cZBdbAHt3ZC24esi4ZA6O6kFdwU1H0ekDmGQGRZCUZAVW3T6W6fzg50jHsdfsdfsfdsfdsfzdixf1RrTFLzV 96ZBWXAZDZD&limit=5000&offset=5000&__after_id=1000456456455"

y 需要提取 [ 和 ] 之间的子字符串我使用下一个代码

NSRange startRange = [strFriends rangeOfString:@"["];
NSRange endRange = [strFriends rangeOfString:@"]"];
NSString *formData = [strFriends  substringWithRange:NSMakeRange(startRange.location,endRange.location)];
NSLog(@"this is the data %@",formData); 

结果是

[ { "name": "Lorena Trujillo", "id": "500144123"}, { "name": "George Arcila", "id": "520311359"}, { "name": "Laura Victoria Mu\u00f1oz Rincon", "id": "528543677"}, { "name": "Camilo Andres Santacoloma Mejia", "id": "529547832"}],"pagin

知道为什么最后这些字符会出现,"pagin 非常感谢

最佳答案

这是因为 startRange 位置和 endRange 位置都指向字符串内的位置 - 但 subStringWithRange 需要一个位置和一个长度 - 所以你需要计算长度要提取的字符串。像这样:

NSUInteger dataLength = endrange.location - startRange.location;
NSString *formData = [strFriends  substringWithRange:NSMakeRange(startRange.location,dataLength)];

除此之外,您确实需要一个 JSON 解析器来解析 JSON。像这样挑选出小的子串将需要大量工作并且很脆弱。

关于objective-c - nsrange.location 不完全是麻烦 makerange 子串 ios objective c,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12203788/

相关文章:

iphone - 使用核心数据撤消管理

ios - 如何使用 View 数组中具有最高高度的 View 进行约束

ios - Swift 在 TableView 中展开 Optional 值时意外发现 nil

ios - CSS translate with vw 在 iOS < 8 上不起作用,解决方法?

iphone - 如何将 CGFloat 设置为 UILabel 的文本属性

ios - 预定义字符串之间的 NSString 子字符串

ios - cellForItemAtIndexPath , sizeForItemAtIndexPath , insetForSectionAtIndex 没有被调用 iOS

ios - 多次加载的 Swift 惰性变量(计算属性?)

objective-c - indentationLevel 属性似乎没有做任何事情?

objective-c - NSTask NSPipe - objective c 命令行帮助