2016-08-17 21 views
0

1つの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例:

(ビスケット

小麦粉)、

(ロールス

牛肉)これは、それがすべてのソートから私を防止することを除いていいと思い

成分値をアルファベット順に並べて、すべての重複値をすべて削除するのを防ぎます "ingr編集者辞書私はJSONファイルを制御しないので、それらを手動で組み合わせることはできません。 誰もが "成分"のすべてのキー値を1つにまとめるための適切なコードをお勧めしますか?

望ましい結果の例:

(ビスケット

小麦粉

ロールス

牛肉)

@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 

答えて

0

ねえ、ここで私の理解あたりとして、私は単純化を持っていますあなたはすべての食材1つのアレイ内のデータと出力の下に取得します。このコードは上記の実行後

@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]; 

    //Replaced Code with sort irritation 

    for(NSString *ingredientsStr in [[jsonDictionary objectForKey:@"locations"] valueForKey:@"ingredients"]){ 
     [locations addObjectsFromArray:[ingredientsStr componentsSeparatedByString:@"\n"]]; 
    } 


    return locations; 
} 

:あなたは以下の言及として全てのシリアル化後に何をしたいあなたのデータ成分 fiedが。

(
    Biscuits, 
    "3 cups All-purpose Flour", 
    "2 Tablespoons Baking Powder", 
    "1/2 teaspoon Salt", 
    "Shortened for example", 
    "12 whole Dinner Rolls Or Small Sandwich Buns (I Used Whole Wheat)", 
    "1 pound Thinly Shaved Roast Beef Or Ham (or Both!)", 
    "Shortened for example" 
) 

希望すると、これが役に立ちます。

+0

上記の回答で問題が解決された場合は、同意してください。 – CodeChanger

+0

お返事ありがとうございます。今夜試して、それがどのように機能するかを教えてくれます。 – Jason

+0

元の質問にすべてのコードが正しく貼り付けられているわけではありません。私は質問を編集し、replaceOccurrencesOfStringを行う欠けているコードを追加しました。この機能を失うことなくコードを実装する方法これを提案する方法であなたの答えを更新できますか?再度、感謝します。 – Jason

関連する問題