解析多个可能的Json响应

问题描述:

我在这里已经看到了一些问题herehere但仍无法进行以下工作。解析多个可能的Json响应

对于任一这些响应其在ex.Message的:

响应1

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

响应2

[ 
    { 
    "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,我期待IsValid - 1返回trueIsValid - 2返回false

,以及针对响应2,我期待IsValid - 1返回falseIsValid - 2返回true

更新

按照David Kujawskidbc给出的建议l通过JArray,并添加我已取得进展的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 *可以是空的 –

+0

我不明白你的意思。你能澄清吗? –

+0

属性* validationErrorType *是一个字符串,字符串是一个引用类型,可以是* null * –

您的问题与JArray vs JObject。如果你真的想把ex.Message作为一个数组来处理,那么你需要遍历数组的子节点。此外,将您的JsonSchema从“数组”更改为“对象”。以下作品如您所述:

 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 
      } 
     }