0
하나 개 NSMutable 배열에 모든 키 값을 결합 :내가 JSON 파일이
{
"locations": [
{ "ingredients" : "Biscuits\n3 cups All-purpose Flour\n2 Tablespoons Baking Powder\n1/2 teaspoon Salt\nShortened for example” },
{ "ingredients" : "12 whole Dinner Rolls Or Small Sandwich Buns (I Used Whole Wheat)\n1 pound Thinly Shaved Roast Beef Or Ham (or Both!)\nShortened for example” },
//many more records. only included 2 for example
]
}
나는 아래의 코드와 배열에 "재료"키를 받고 있어요,하지만 NSLog 각 "재료"를 반환 별도의 키 .
NSLog 예 :
(비스킷
밀가루),
(롤스
쇠고기)이이 모든 분류에서 저를 방지하는 것을 제외하고 괜찮을 것
성분 값을 사전 순으로 정렬하고 모든 중복 값을 모두 제거하지 못하도록합니다. " edients "사전. JSON 파일을 제어하지 않기 때문에 수동으로 결합 할 수 없습니다. 누구든지 올바른 "모든 재료"키 값을 하나의 코드로 결합하는 코드를 권장 할 수 있습니까?
원하는 결과 예 :
(비스킷
밀가루
롤스
쇠고기)
@implementation JSONLoaderIngreds
- (NSArray *)ingredientsFromJSONFile:(NSURL *)url {
// Create a NSURLRequest with the given URL
NSURLRequest *request = [NSURLRequest requestWithURL:url
cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData
timeoutInterval:30.0];
// Get the data
NSURLResponse *response;
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil];
// NSData *jsonData = [NSJSONSerialization dataWithJSONObject:arr options:NSJSONWritingPrettyPrinted error:&error];
// Now create a NSDictionary from the JSON data
NSDictionary *jsonDictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
// Create a new array to hold the locations
NSMutableArray *locations = [[NSMutableArray alloc] init];
// Get an array of dictionaries with the key "locations"
NSMutableArray *array = [jsonDictionary objectForKey:@"locations"];
for(NSDictionary *dict in array) {
NSString *string = [dict objectForKey:@"ingredients"];
NSMutableString *removewords = [string mutableCopy];
CFStringTrimWhitespace((__bridge CFMutableStringRef) remove words);
//\\//\\//\\//\\//\\EDIT Added Code Below//\\//\\//\\//\\//\\//\\
[removewords replaceOccurrencesOfString:@"[0-9]" withString:@"" options:NSRegularExpressionSearch range:NSMakeRange(0, [removewords length])];
[removewords replaceOccurrencesOfString:@"tablespoons " withString:@"" options:1 range:NSMakeRange(0, [removewords length])];
[removewords replaceOccurrencesOfString:@"cups " withString:@"" options:1 range:NSMakeRange(0, [removewords length])];
[removewords replaceOccurrencesOfString:@"cup " withString:@"" options:1 range:NSMakeRange(0, [removewords length])];
//\\//\\//\\//\\//\\EDIT Added Code Above//\\//\\//\\//\\//\\//\\
NSArray *brokenIntoParts = [removewords componentsSeparatedByString:@"\n"];
NSArray *sortedArray = [brokenIntoParts sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
for (NSString *ingredient in sortedArray) {
[locations addObject:ingredient];
}
NSLog(@"%@", sortedArray);
}
return locations;
}
@end
위의 답변으로 문제가 해결 된 경우 동의 해주세요. – CodeChanger
답장을 보내 주셔서 감사합니다. 오늘 밤 시도하고 어떻게 작동하는지 알려주지. – Jason
나는 원래의 질문에 정확하게 붙여 넣은 모든 코드가 아니라는 것을 깨달았지만 그것을 작동시키지 못했습니다. 질문을 편집하고 replaceOccurrencesOfString을 수행하는 누락 된 코드를 추가했습니다. 이 기능을 잃지 않고 코드를 구현하는 방법은 무엇입니까? 이 질문에 대한 답변을 어떻게 업데이트 할 수 있습니까? 다시 한번 감사드립니다. – Jason