2017-09-05 3 views
0

여기에 이미 많은 질문을 보았습니다. herehere하지만 여전히 다음 작업을 수행 할 수 없습니다. ex.Message에 이러한 응답 중 하나를 들어 여러 개의 가능한 Json 응답을 구문 분석

:

응답 한

[ 
    { 
    "validationErrorType": "WrongType", 
    "message": "Validation error of type WrongType", 
    "errorType": "ValidationError" 
    } 
] 

응답이

[ 
    { 
    "message": "Validation error of type WrongType:", 
    "errorType": "ValidationError" 
    } 
] 

내가 동적으로이 구문 분석을 시도하고있다 다음과 같습니다 :

JArray parsedJObject = JArray.Parse(ex.Message); 

JSchema oldSchema = JSchema.Parse(@" 
      { 
       'type': 'array', 
       'properties': { 
        'message': {'type': 'string'}, 
        'errorType': {'type': 'string'} 
      }, 
      'additionalProperties': false 
      }"); 

JSchema graphQlSchema = JSchema.Parse(@" 
      { 
       'type': 'array', 
       'properties': { 
        'validationErrorType': {'type': 'string'}, 
        'message': {'type': 'string'}, 
        'errorType': {'type': 'string'} 
      }, 
      'additionalProperties': false 
      }"); 

if (parsedJObject.IsValid(oldSchema)) // IsValid - 1 
{ 
    // Do stuff 
} 
else if (parsedJObject.IsValid(graphQlSchema)) // IsValid - 2 
{ 
    // Do stuff 
} 

그러나 IsValid() 호출은 두 응답 중 하나를 true로 반환합니다. 여기서 내가 뭘 잘못하고 있니? 응답의 경우 1

, 나는

그리고 응답 2 false, 나는 돌아 falseIsValid - 2을 반환 IsValid - 1을 기대하고있어 반환 trueIsValid - 2을 반환 IsValid - 1을 기대하고 있습니다 true

업데이트

David Kujawskidbc을 l JArray를 통해 oop을 실행하고 required 속성을 추가합니다.

내 업데이트 된 코드는 아래에 있지만 중첩 된 locations 개체가있는 스키마의 유효성을 검사하는 데 여전히 어려움이 있습니다.

응답

[ 
    { 
    "validationErrorType": "WrongType", 
    "locations": [ 
     { 
     "line": 4, 
     "column": 1 
     } 
    ], 
    "message": "Validation error of type WrongType", 
    "errorType": "ValidationError" 
    } 
] 

스키마 정의 :

JSchema graphQlSchema = JSchema.Parse(@" 
    { 
     'type': 'object', 
     'properties': 
     { 
      'validationErrorType': {'type': 'string'}, 
      'locations':   
       { 
        'type': 'object', 
        'properties': 
        { 
         'line': {'type': 'string'}, 
         'column': {'type': 'string'} 
        } 
       }, 
      'message':    {'type': 'string'}, 
      'errorType':   {'type': 'string'} 
     }, 
     'additionalProperties': false, 
     'required': ['message', 'errorType', 'validationErrorType', 'locations'] 
    }"); 

구문 분석 응답

JArray parsedJObject = JArray.Parse(ex.Message); 

foreach (JToken child in parsedJObject.Children()) 
{ 
    if (child.IsValid(graphQlSchema)) // Not resolving to true 
    { 
     var graphQlSchemaDef = new[] 
         { 
          new 
          { 
           validationErrorType = string.Empty, 
           locations = new 
            { 
             line = string.Empty, 
             column = string.Empty 
            }, 
           message = string.Empty, 
           errorType = string.Empty 
          } 
         }; 

     var exceptionMessages = JsonConvert.DeserializeAnonymousType(ex.Message, graphQlSchemaDef); 

     foreach (var message in exceptionMessages) 
     { 
      // Do stuff 
     } 
    } 
} 
+0

그들은 유사합니다. * validationErrorType *은 null 일 수 있습니다. –

+0

무슨 뜻인지 이해가 안됩니다. 제발 좀 더 명확하게 해줄 수 있니? –

+0

속성 * validationErrorType *은 문자열이고 string은 참조 유형이며 * null 일 수 있습니다. * –

답변

2

문제 JArray 대 JObject입니다. ex.Message를 배열로 처리하려면 배열의 자식을 반복해야합니다. 또한 JsonSchema를 "array"에서 "object"로 변경하십시오. 귀하가 설명한대로 다음과 같이 작동합니다 :

 JArray parsedJObject = JArray.Parse(ex.Message); 

     JSchema oldSchema = JSchema.Parse(@" 
     { 
      'type': 'object', 
      'properties': { 
       'message': {'type': 'string'}, 
       'errorType': {'type': 'string'} 
      }, 
      'additionalProperties': false 
     }"); 

     JSchema graphQlSchema = JSchema.Parse(@" 
     { 
      'type': 'object', 
      'properties': { 
       'validationErrorType': {'type': 'string'}, 
       'message': {'type': 'string'}, 
       'errorType': {'type': 'string'} 
      }, 
      'additionalProperties': false 
     }"); 

     foreach (var item in parsedJObject.Children()) 
     { 
      if (item.IsValid(oldSchema)) // IsValid - 1 
      { 
       // Do stuff 
      } 
      else if (item.IsValid(graphQlSchema)) // IsValid - 2 
      { 
       // Do stuff 
      } 
     }