2017-09-24 16 views
0

ここにNoob。私は最近、客観的なCで作業を始めました。現在、私は辞書のコンセプトに固執しています。私は、以下に示すようにJSONオブジェクトを作成したい:辞書の配列を持つ辞書を作成

{"UserData": { 
       "Name": Mike Smith, 
       "Age": 32, 
       "category": [1,2,3], 
       "Weekly Data": [ 
           {"Monday" : [1.0,2.0,3.0]}, 
           {"Tuesday": [1.0,2.0,3.0]} 
          ] 
       } 
} 

私は望ましい結果が得られていない次のコードを書きました。私は誰かが私を助けることができるかと思います。

-(NSString*)populateUserPreferences 
{ 
    NSMutableDictionary *dict = [[NSMutableDictionary alloc] init]; 
    NSMutableArray *categorydata = [[NSMutableArray alloc] init]; 
    NSMutableArray *weeklydata = [[NSMutableArray alloc] init]; 

    for (int i=0;i<4; i++) 
    { 
     [categorydata addObject:[NSNumber numberWithInt:i]]; 
    } 


     NSMutableArray *mondaydata = [[NSMutableArray alloc] init]; 
     for (int j=0; j<3; j++) 
     { 
      [mondaydata addObject:[NSNumber numberWithInt:j]]; 
     } 

     NSMutableArray *tuesdaydata = [[NSMutableArray alloc] init]; 
     for (int j=0; j<3; j++) 
     { 
      [tuesdaydata addObject:[NSNumber numberWithInt:j]]; 
     } 


     NSDictionary *monday = [NSDictionary dictionaryWithObject:mondaydata]; 
     NSDictionary *tuesday = [NSDictionary dictionaryWithObject:tuesdaydata]; 

     [weeklydata addObject: monday ]; 
     [weeklydata addObject: tuesday ]; 

    } 

    [dict setObject:[NSString stringWithFormat:"Mike Smith"] forKey:@"Name"]; 
    [dict setObject:[NSNumber numberWithInteger:32.0] forKey:@"Age"]; 

    [dict setObject:categorydata forKey:@"category"]; 
    [dict setObject:weeklydata forKey:@"Weekly Data"]; 

    NSString * userdata = [dict JSONRepresentation]; 
    NSLog(request); 

    NSDictionary *userdataJson = [NSDictionary dictionaryWithObject:dict forKey:@"userData"]; 

    return [userdataJson JSONRepresentation]; 
} 

ありがとうございます。月曜日と火曜日の辞書を作成するときに

Apoorva

+0

期待される結果は*ですか?代わりにあなたが得ている結果は何ですか? –

答えて

0

間違いがあります。

// mondaydata & tuesday is just array. 
NSDictionary *monday = [NSDictionary dictionaryWithObject:mondaydata]; 
NSDictionary *tuesday = [NSDictionary dictionaryWithObject:tuesdaydata]; 

このコードは、辞書を正しく割り当てなかったため誤っています(辞書のキーはどこですか)。代わりに、次のようにする必要があります。

NSDictionary *mondayDict = [[NSDictionary alloc] init]; 
[mondayDict setObject:mondaydata forKey:"Monday"]; 
NSDictionary *tuesdayDict = [[NSDictionary alloc] init]; 
[tuesdayDict setObject:tuesdaydata forKey:"Tuesday"]; 

次に、mondayDictとtuesdayDictを配列weeklydataに追加できます。

ps。あなたの変数に意味のある名前を付けてください。たとえば、mondaydataは十分に記述的ではありません。たとえば、mondayArrを使用する必要があります。簡単に識別するために配列です。共有するだけの通常のコーディング練習。

0
NSDictionary * dict = @{@"UserData": @{ 
           @"Name": @"Mike Smith", 
           @"Age": @32, 
           @"category": @[@1,@2,@3], 
           @"Weekly Data": @[ 
             @{@"Monday" : @[@1.0,@2.0,@3.0]}, 
             @{@"Tuesday": @[@1.0,@2.0,@3.0]} 
             ] 
           } 
         }; 
NSError * error = nil; 
NSData * data = [NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:&error]; 
if (error) { 
    NSLog(@"%@", [error localizedDescription]); 
} else { 
    // Do what you want 
} 
関連する問題