从Brewerydb API读取JSON数据时出错

时间:2015-10-06 18:57:57

标签: c# .net json

我正在尝试使用JSON收集啤酒厂信息的第一个API调用,并收到以下错误:

  

Newtonsoft.Json.dll中出现未处理的“Newtonsoft.Json.JsonSerializationException”类型异常

     

附加信息:无法将当前JSON对象(例如{“name”:“value”})反序列化为类型'System.Collections.Generic.List`1 [Brewery.Adjunct]',因为该类型需要JSON数组(例如[1,2,3])正确反序列化。

     

要修复此错误,请将JSON更改为JSON数组(例如[1,2,3])或更改反序列化类型,使其成为普通的.NET类型(例如,不是像整数这样的基本类型,而不是可以从JSON对象反序列化的集合类型,如数组或List)。 JsonObjectAttribute也可以添加到类型中以强制它从JSON对象反序列化。

     

路径'消息',第1行,第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方法?

1 个答案:

答案 0 :(得分:1)

基于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; }
    }
}