2016-08-16 6 views
0

は、私のようなスキーマに対してJSONを検証する必要があります。C#用のJsonスキーマ検証ツールがありますか?

bool isValid = SomeJsonSchemaValidator(responseContentInJsonFormat, jsonSchema); 

は、誰もがこれを行うことができます。ネットNugetパッケージか何かを知っています ? 私はPOSTMANがTinyValidatorを使用していることを知っています。しかし、私はこれをC#NUnit Testsから行う必要があります。

+0

http://stackoverflow.com/questions/14977848/how-to-make-sure-that-string-is-valid-json-using-json-net – adrianbanks

+0

ライブラリは必要ありません。 DataContractJavascriptSerializerを使用し、必要に応じてすべてのプロパティにマークを付けてください – Steve

答えて

0

は、それはJson.NETで行うことができます。

string schemaJson = @"{ 
    'description': 'A person', 
    'type': 'object', 
    'properties': 
    { 
    'name': {'type':'string'}, 
    'hobbies': { 
     'type': 'array', 
     'items': {'type':'string'} 
    } 
    } 
}"; 

JsonSchema schema = JsonSchema.Parse(schemaJson); 

JObject person = JObject.Parse(@"{ 
    'name': 'James', 
    'hobbies': ['.NET', 'Blogging', 'Reading', 'Xbox', 'LOLCATS'] 
}"); 

bool valid = person.IsValid(schema); 

使用このエラー・メッセージを参照してください必要がある場合:

JObject person = JObject.Parse(@"{ 
    'name': null, 
    'hobbies': ['Invalid content', 0.123456789] 
}"); 

IList<string> messages; 
bool valid = person.IsValid(schema, out messages); 
// false 
// Invalid type. Expected String but got Null. Line 2, position 21. 
// Invalid type. Expected String but got Float. Line 3, position 51. 

出典:http://www.newtonsoft.com/json/help/html/JsonSchema.htm

関連する問題