2014-11-21 3 views
0

아래 객석에서 작성해야 할 스 니펫이 있습니다. c.objective-c로 중첩 된 json 객체를 생성합니다.

{ 
    "expression": "???=n1+n2+n3+n4", 
    "terms": [ 
    { 
     "name": "termsTitle", 
     "term": [ 
     { 
      "name": "answer", 
      "value": "???" 
     } 
     ] 
    } 
    ] 
} 

는 여기에게 내가 가까이 모르지만, 어떤 이유로,이 나를 위해 작동하지 않는 생각

NSMutableArray *terms= [[NSMutableArray alloc] init]; 

NSArray *keysTerms = [[NSArray alloc] initWithObjects:@"name", @"term", nil]; 


NSMutableArray *term= [[NSMutableArray alloc] init]; 

NSArray *objectsTerms = [[NSArray alloc] initWithObjects:@"answer",term, nil]; 

NSDictionary *termsDict = [[NSMutableDictionary alloc] initWithObjects:objectsTerms forKeys:keysTerms]; 

[terms addObject:termsDict]; 

NSDictionary *statement = [[NSDictionary alloc] 
          initWithObjectsAndKeys:expression,@"expression", 
          terms,@"terms", 
          nil]; 

을 만들 내 시도입니다. 나는 어떤 도움을 주셔서 감사합니다. 미리 감사드립니다. 당신은 당신이 더 자세한 코드를 작성할 필요가 구조에 약간의 문제가있는 경우

+1

[의 NSNumber numberWithInteger : 용어]이 어떻게 할 수 있습니까? 용어는 정수가 아니기 때문에 배열입니다. – Alexi

+0

죄송합니다. – ilteris

답변

1

NSDictionary* termDict = [NSDictionary dictionaryWithObjects:@[@"answer",@"???"] forKeys:@[@"name",@"value"]]; 

    NSArray* termArray = [NSArray arrayWithObjects:termDict, nil]; 

    NSDictionary* termsDict = [NSDictionary dictionaryWithObjects:@[@"termsTitle",termArray] forKeys:@[@"name",@"term"]]; 

    NSMutableArray* terms = [NSMutableArray arrayWithObjects:termsDict, nil]; 

    NSDictionary* result = [NSDictionary dictionaryWithObjects:@[@"???=n1+n2+n3+n4",terms] forKeys:@[@"expression",@"terms"]]; 

    NSLog(@"result: %@",[result description]); 

[결과

2014-11-21 15:03:14.396 Answering_question[23716:113224] result: { 
    expression = "???=n1+n2+n3+n4"; 
    terms =  (
       { 
      name = termsTitle; 
      term =    (
           { 
        name = answer; 
        value = "???"; 
       } 
      ); 
     } 
    ); 
} 
+0

Thanks @siu !!! 멋지게 보이고 객관적인 -c에서 중첩 된 JSON을 만드는 방법을 보여주는 좋은 예입니다. 다시 한번 감사드립니다. – ilteris

+0

@ilteris 당신을 환영합니다;) –

0

을 작동합니다. 이처럼 :

NSMutableDictionary *mainDict= [NSMutableDictionary new]; 
[mainDict setObject:@"???=n1+n2+n3+n4" forKey:@"expression"]; 

NSMutableArray *terms = [NSMutableArray new]; 

NSMutableDictionary *term = [NSMutableDictionary new]; 
[term setObject:@"name" forKey:@"termsTitle"]; 

NSMutableArray *subTerms = [NSMutableArray new]; 
NSMutableDictionary *subTerm = [NSMutableDictionary new]; 
[subTerm setObject:@"answer" forKey:@"name"]; 
[subTerm setObject:@"???" forKey:@"value"]; 
[subTerms addObject:subTerm]; 

[term setObject:subTerms forKey:@"term"]; 

[terms addObject:term]; 

[mainDict setObject:terms forKey:@"terms"]; 

NSData *jsonData = [NSJSONSerialization dataWithJSONObject:mainDict 
                options:NSJSONWritingPrettyPrinted 
                error:nil]; 
NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding]; 

출력 :

{ 
    "expression" : "???=n1+n2+n3+n4", 
    "terms" : [ 
    { 
     "termsTitle" : "name", 
     "term" : [ 
     { 
      "name" : "answer", 
      "value" : "???" 
     } 
     ] 
    } 
    ] 
}