2014-11-09 2 views
0

쓰기 If VBA If 문을 쓰려고합니다.
특정 JSON 노드가 있는지 확인하고 있습니다.IF, ELSEIF ELSE

첫 번째 노드, 연속 노드 및 잘못된 빈 노드의 세 가지 유형이 있습니다.

처음있을 때 계속 매달려 있습니다. 내가 더 MediaType 아이가없는, 때로는 추측거야

If Not jSonRoot.child("MediaType").child("first") Is Nothing Then 
    'First Pass 
ElseIf Not jSonRoot.child("second") Is Nothing Then 
    'Continuation 
Else 
    'Json is nothing, malformed. 
End If 
+1

"는 전화를 끊었다"어떻게? ** 모든 경우 **에'jSonRoot'의'MediaType' 하위 항목이 있다는 것을 보장합니까? (왜냐하면 코드는'.child ("MediaType")'의 결과에'.child'를 호출함으로써 그 전제에 의존하기 때문입니다.) –

답변

1

; 이 경우 .child("first")Nothing에 전화하려고하기 때문에 첫 번째 If이 먼저 터질 것입니다.

만약 내가 해요 권리, 슬프게도, 당신은 VBA가 And에 대한 단락이 없기 때문에 밖으로 별도의 If 문으로 그 첫 번째 조건을 분리해야합니다 :

If Not jSonRoot.child("MediaType") Is Nothing Then 
    If Not jSonRoot.child("MediaType").child("first") Is Nothing Then 
     'First Pass 
    Else 
     'Handle case where there's a MediaType but no .child("first") 
    End If 
ElseIf Not jSonRoot.child("second") Is Nothing Then 
    'Continuation 
Else 
    'Json is nothing, malformed. 
End If 
관련 문제