从Brewerydb API读取JSON数据时出错
本文关键字:数据 出错 JSON 读取 Brewerydb API | 更新日期: 2023-09-27 18:16:40
我正在尝试使用JSON收集啤酒厂信息并接收以下错误的第一个API调用:
类型为'Newtonsoft.Json '的未处理异常。JsonSerializationException'在Newtonsoft.Json.dll中发生
附加信息:无法将当前JSON对象(例如{"name":"value"})反序列化为类型'System.Collections.Generic.List ' 1[Brewery. xml]。因为该类型需要一个JSON数组(例如[1,2,3])来正确反序列化。
要修复此错误,要么将JSON更改为JSON数组(例如[1,2,3]),要么更改反序列化类型,使其成为可以从JSON对象反序列化的普通。net类型(例如,不是像整数这样的原始类型,也不是像数组或列表这样的集合类型)。还可以将JsonObjectAttribute添加到类型中,以强制它从JSON对象进行反序列化。
路径'message',第一行,位置11。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
namespace Brewery
{
class Adjunct
{
//properties
public int id { get; set; }
public string name { get; set; }
public string description { get; set; }
public string category { get; set; }
public string catDisplay { get; set; }
//create list of Adjunct objects
public List<Adjunct> adjuncts{ get; set; }
//address with query for all adjunct items
const string address = "http://api.brewerydb.com/v2/?[MY KEY]/adjunct";
//fill adjuncts with deserialized json data
public void GetBeer(string beer)
{
//initialize beer list
adjuncts = new List<Adjunct>();
//build connection with query and return string
//***static class Connect uses System.net to create web request and web response objects***
string result = Connect.GetConnection(address);
//get list of Adjunct objects
adjuncts = JsonConvert.DeserializeObject<List<Adjunct>>(result);
}
//get Adjunct string
public string PrintData(string sep)
{
return "name: " + name + sep + "id: " + id + sep + "description: " + description;
}
//search adjunct by id
public Adjunct AdjunctByID(int _id)
{
foreach (Adjunct ad in adjuncts)
{
if (_id == id)
{
return ad;
}
}
return null;
}
}
我需要使用不同的JSON吗?净方法?
根据brewerydb文档,您的类应该是这样的:
public class Adjunct
{
public int id { get; set; }
public string name { get; set; }
public string category { get; set; }
public string categoryDisplay { get; set; }
public string createDate { get; set; }
}
public class AdjunctsReply
{
public int currentPage { get; set; }
public int numberOfPages { get; set; }
public int totalResults { get; set; }
public List<Adjunct> adjuncts { get; set; }
public string status { get; set; }
}
你可以像这样反序列化JSON:
reply = JsonConvert.DeserializeObject<AdjunctsReply>(result);
例如:
public void GetBeer(string beer)
{
//initialize beer list
AdjunctsReply reply;
//build connection with query and return string
//***static class Connect uses System.net to create web request and web response objects***
string result = Connect.GetConnection(address);
//get list of Adjunct objects
reply = JsonConvert.DeserializeObject<AdjunctsReply>(result);
foreach(var adjunct in reply.adjuncts)
{
Console.WriteLine(adjunct.name);
}
}
编辑完整的工作示例。只需插入密钥并将其作为控制台应用程序运行即可。
using System;
using System.Collections.Generic;
using System.Net;
using Newtonsoft.Json;
namespace WebClientDownloadMp4
{
class Program
{
static void Main(string[] args)
{
// Insert your key here
var key = "0000000000000000000000000000";
var client = new WebClient();
var reply = client.DownloadString(@"http://api.brewerydb.com/v2/adjuncts?key=" + key);
var adjunctsReply = JsonConvert.DeserializeObject<AdjunctsReply>(reply);
Console.WriteLine("Current page: " + adjunctsReply.currentPage);
Console.WriteLine("Total pages: " + adjunctsReply.numberOfPages);
Console.WriteLine("Total results: " + adjunctsReply.totalResults);
foreach (var adjunct in adjunctsReply.Adjuncts)
{
Console.WriteLine("Id {0}: {1}", adjunct.id, adjunct.name);
}
}
}
public class Adjunct
{
public int id { get; set; }
public string name { get; set; }
public string category { get; set; }
public string categoryDisplay { get; set; }
public string createDate { get; set; }
}
public class AdjunctsReply
{
public int currentPage { get; set; }
public int numberOfPages { get; set; }
public int totalResults { get; set; }
[JsonProperty("data")]
public List<Adjunct> Adjuncts { get; set; }
public string status { get; set; }
}
}