错误与Brewerydb API
我试图使用JSON我的第一个API调用来收集啤酒厂信息,并收到以下错误读取JSON数据:错误与Brewerydb API
An unhandled exception of type 'Newtonsoft.Json.JsonSerializationException' occurred in Newtonsoft.Json.dll
Additional information: Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[Brewery.Adjunct]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.
To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object.
Path 'message', line 1, position 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.NET方法?
基础上brewerydb documentation你的类应该是这样的:
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; }
}
}
那么,Adjunct类也可以包含方法,还是仅仅匹配JSON对象的属性? – dachizzle37
它也可以包含方法。 – venerik
那么,我需要在AdjunctReply类中使用JSON? – dachizzle37
这将有助于如果您包括您试图反序列化的JSON。但我的猜测正是错误信息所说的。您试图将一个属性反序列化到列表'adjucts'中,但它不是JSON中的数组。 –
我怀疑你的辅助课是不正确的。您可以在这里从JSON生成C#类:[http://json2csharp.com](http://json2csharp.com)。 – venerik
这里有一个例子:{id:876,8。名称:“酸混合”,9。类别:“misc”,10。 categoryDisplay:“Miscellaneous”,11。 createDate:“2013-06-14 12:19:03”12。 }, – dachizzle37