[
{
"key": "",
"value": ""
},
{
"key": "",
"value": ""
}
]
我需要驗證他的結構
var client = new RestClient("http://...");
client.Timeout = -1;
var request = new RestRequest(Method.GET);
request.AddHeader("Authorization", "...");
IRestResponse response = client.Execute(request);
Console.WriteLine(response.Content);
string schemaJson = @"{'type':'array','items': {'type':'object','properties':{'Key':{'anyOf':[{'type':'string'},{'type':'null'}]},'Value':{'anyOf':[{'type':'string'},{'type':'null'}]}},'required':['Key','Value']}}";
JsonSchema schema = JsonSchema.Parse(schemaJson);
JObject person = JObject.Parse(response.Content);
bool valid = person.IsValid(schema);
Console.WriteLine(valid);
請問 我驗證結構的格式有寫錯嗎
JsonSchema schema = JsonSchema.Parse(schemaJson);會有錯誤(System.ArgumentException: 'Can not convert Array to Boolean.'
)
這個該怎麼解決
我個人建議用Newtonsoft.JSON來解析json(比較順手)
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
namespace MVC_A
{
class Program
{
static void Main(string[] args)
{
using (WebClient wc = new WebClient())
{
var res = wc.DownloadString(new Uri("http://music.xxx.com/n.json"));
// 例
var myobjs = JsonConvert.DeserializeObject<List<schemOBJ>>(res);
foreach (var obj in myobjs)
{
Console.WriteLine($"key:{obj.key}\tvalue:{obj.value}");
}
Console.ReadKey();
}
}
}
class schemOBJ
{
public string key { get; set; }
public string value { get; set; }
}
}