【发布时间】:2020-06-09 23:21:56
【问题描述】:
编辑:我发现这是 JArray,我需要 JObject [],但我需要一些关于如何使它成为可能的想法。
我遇到了无法从 API 获取数据的问题 - https://api.itbook.store/
尝试 url 时出错 - https://api.itbook.store/1.0/ - System.Net.WebException: '远程服务器返回错误:(404) Not Found。'
当我尝试使用此 url -https://api.itbook.store/1.0/search/mongodb 加载页面示例中的几本书时 - 我收到以下消息:Newtonsoft.Json.JsonReaderException: 'Error reading JArray from JsonReader.当前 JsonReader 项不是数组:StartObject。路径'',第 1 行,位置 1。'
public KnjiznicaRepo()
{
string url = "https://api.itbook.store/1.0/";
string json = CallRestMethod(url);
JArray jsonObject = JArray.Parse(json);
foreach (JObject item in jsonObject)
{
knjiznicas.Add(new Knjiznica
{
NazivKnjige = (string)item.GetValue("title"),
Podnaziv = (string)item.GetValue("subtitle"),
ISBN13 = (string)item.GetValue("isbn13"),
Cijena = (string)item.GetValue("price"),
Link = (string)item.GetValue("url")
});
}
}
public static string CallRestMethod(string url)
{
HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create(url);
webrequest.Method = "GET";
webrequest.ContentType = "application/x-www-form-urlencoded";
HttpWebResponse webresponse = (HttpWebResponse)webrequest.GetResponse();
Encoding enc = System.Text.Encoding.GetEncoding("utf-8");
StreamReader responseStream = new StreamReader(webresponse.GetResponseStream(), enc);
string result = string.Empty;
result = responseStream.ReadToEnd();
webresponse.Close();
return result;
}
【问题讨论】: