2015-01-11 2 views
-3

here이라는 JSON 응답이 있습니다.이 특정 JSON 응답을 구문 분석하는 방법

내가 NSArrayNSDictionary를 사용하여 과거에 구문 분석 된 JSON의 reponses을했지만, JSON 응답의 인코딩에 대한 설명서 (here을 발견가) 혼란이다. 예를 들어

,이 샘플 님의 질문에 답변

{ 
    "response": { 
     "success": 1, 
     "current_time": 1392754263, 
     "raw_usd_value": 0.31, 
     "items": { 
      "Kritzkrieg": { 
       "defindex": { 
        "0": 35 
       }, 
       "prices": { 
        "11": { 
         "Tradable": { 
          "Craftable": { 
           "0": { 
            "currency": "keys", 
            "value": 32, 
            "value_high": 34, 
            "last_update": 1388253643, 
            "difference": 21 
           } 
          } 
         } 
        }, 
        "3": { 
         "Tradable": { 
          "Craftable": { 
           "0": { 
            "currency": "metal", 
            "value": 0.33, 
            "last_update": 1379423777, 
            "difference": -0.11 
           } 
          } 
         } 
        }, 
        "6": { 
         "Tradable": { 
          "Craftable": { 
           "0": { 
            "currency": "metal", 
            "value": 0.05, 
            "last_update": 1336410088, 
            "difference": 0 
           } 
          }, 
          "Non-Craftable": { 
           "0": { 
            "currency": "metal", 
            "value": 0.05, 
            "last_update": 1362791812, 
            "difference": 0.03 
           } 
          } 
         } 
        } 
       } 
      }, 
     } 
    } 
} 

특정 NSDictionaries 키 "Craftable", "비 Craftable"를 포함, 등

내 응답을 구문 분석에 대한 이동

방법 이 일을 성취 할 수 있습니까? 지금까지

내 코드 :이 사전에 존재하지 않는 키에 대한

+ (NSArray *)groupsFromJSON:(NSData *)objectNotation error:(NSError **)error 
{ 
    NSError *localError = nil; 
    NSDictionary *parsedObject = [NSJSONSerialization JSONObjectWithData:objectNotation options:0 error:&localError]; 

    if (localError != nil) { 
     *error = localError; 
     return nil; 
    } 

    NSMutableArray *groups = [[NSMutableArray alloc] init]; 
    NSDictionary *results = [parsedObject objectForKey:@"response"]; 
    NSDictionary *items = [results objectForKey:@"items"]; 

    for (NSDictionary *groupDic in items) { 
     TF2 *tf2 = [[TF2 alloc] init]; 
     NSDictionary *groupDick = [items objectForKey:groupDic]; 
     NSDictionary *prices = [groupDick valueForKey:@"prices"]; 
     for(NSDictionary *groupDicky in prices) { 
      NSDictionary *groupDic1 = [groupDicky objectForKey:prices]; 
      for(NSDictionary *groupDic2 in groupDic1) { 
       NSDictionary *tradable = [groupDic2 valueForKey:@"Tradable"]; 
       for(NSDictionary *groupDic3 in tradable) { 
        NSDictionary *craftable = [groupDic3 valueForKey:@"Craftable"]; 
        NSDictionary *uncraftable = [groupDic3 valueForKey:@"Non-Craftable"]; 

       NSDictionary *untradable = [groupDic2 valueForKey:@"Non-Tradable"]; 
       for(NSDictionary *groupDick4 in tradable) { 
        NSDictionary *craftable2 = [groupDick4 valueForKey:@"Craftable"]; 
        NSDictionary *uncraftable2 = [groupDick4 valueForKey:@"Non-Craftable"]; 
       } 
      } 

     } 
     //[groups addObject:list]; 
    } 

    return groups; 
} 
+1

정확히이 JSON을 추출하려고합니까? – Shai

+0

@shai 응답에 "currency"값을 넣으려고합니다. –

+0

JSOM을 NSJSONSerialization으로 구문 분석합니다. 그 후에 Objective-C 배열과 사전 객체의 구조를 가지면 더 이상 JSON이 아닙니다. IOW에서는 JSON을 파싱하지 않고 네이티브 객체에서 항목에 액세스합니다. – zaph

답변

1

NSDictionary 반환 nil는 :

NSDictionary *craftable = [groupDic3 valueForKey:@"Craftable"]; 
// craftable is nil here 

그것은 어떤 다음 통화뿐만 아니라 nil을 반환 것을 의미한다 :

NSDictionary* dict = [craftable valueForKey:@"0"]; // nil 
NSString* currency = [dict valueForKey:@"currency"]; // nil 

즉, "Craftable" 또는 "Non-Craftable"currency이 있으면이 값을 얻습니다. 그렇지 않으면 nil이됩니다. 필요한 데이터를 사용할 수 있는지 이해하기에 충분하지 않습니까?

+0

NSString에는 "Craftable", "Non-Craftable"또는 둘 다에 대한 NSDictionary가 포함되어 있는지에 따라 NSString "currency"에 여러 값이 있습니다. 이 NSDictionary가 있는지 if 문을 사용하여 확인해야합니까? –

+0

죄송합니다. 질문을받지 못했습니다. 필요한 섹션 ("Craftable"또는 "Non-Craftable")에서 통화 값을 추출하고 해당 값이 0이 아닌 경우이를 사용하십시오. 두 섹션의 값이 필요하면 배열이나 사전에 저장하거나 여러 변수에 저장하십시오. –

관련 문제