我正在使用 Mantle 解析来自 Yelp 的一些 JSON 数据。
对于每个返回的业务,我都会得到一个 NSArray
类别。这将是一个例子:
yelpCategories = (
(
"Wine Bars",
"wine_bars"
),
(
"Ice Cream & Frozen Yogurt",
icecream
)
);
yelpCategories 是我保存的数组的名称。稍后我试图将数组解析为字符串:
NSMutableString *yelpCats = [[NSMutableString alloc] init];
for (NSObject * obj in business.yelpCategories)
{
[yelpCats appendString:[NSString stringWithFormat:@"%@,",[obj description]]];
}
问题出在上面。我被返回一个字符串就像“(”所以我必须错误地访问数组。我怎样才能正确访问每个对象,理想情况下我会寻找结束字符串o是@“Wine Bars,Ice Cream&Frozen Yogurt” .
编辑
The categories array: (
(
Pubs,
pubs
)
)
最终编辑 - 提议的解决方案
for (NSArray *cats in business.yelpCategories)
{
NSString *category = [cats objectAtIndex:0];
if ([category length] > 0) {
category = [category substringToIndex:[category length] - 1];
}
if (cats == business.yelpCategories.lastObject) {
[yelpCats appendString:[NSString stringWithFormat:@"%@",category]];
} else {
[yelpCats appendString:[NSString stringWithFormat:@"%@, ",category]];
}
}
cell.yelpCategories.text = yelpCats;
最佳答案
使用对象的描述可以提供在调试器中看到的内容,其中包括额外的回车符。
你想要做的是这样的:
yelpCats = [yelpCategories componentsJoinedByString:@", "];
关于ios - 解析 JSON 数据并处理数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23446564/