Rien
Rien

Reputation: 458

c# Deserialize json into List

I have this code that retrieves Json string from API link. The Json is deserialized and returned to a textbox. This works perfectly as long as there is only 1 Json value, being returned more than 1 value it crashes with this error:

Additional information: Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'GW2_tradingPost.RootObject' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.

To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array.

Doing my research this happens because there is nowhere to deposit the Json since its not an List.

I've have tried this code and various similar ones.

List<RootObject> list = JsonConvert.DeserializeObject<List<RootObject>>(jsonReader.ToString());
return list;

This will return with error:

Error 1 Cannot implicitly convert type 'System.Collections.Generic.List' to 'GW2_tradingPost.RootObject' e:\mega\gw2_tradingpost\gw2_tradingpost\api_request.cs 34

Which i dont fully understand what it means.

Here is my full code.

api_Request.cs

public class api_Request
    {
        public RootObject GetApi(string url)
        {

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);

            try
            {
                WebResponse response = request.GetResponse();
                using (Stream responseStream = response.GetResponseStream())
                {
                 StreamReader reader = new StreamReader(responseStream, Encoding.UTF8);
                var jsonReader = new JsonTextReader(reader);
                var serializer = new JsonSerializer();
                return serializer.Deserialize<RootObject>(jsonReader);
            }
            }
            catch (WebException ex)
            {
                WebResponse errorResponse = ex.Response;
                using (Stream responseStream = errorResponse.GetResponseStream())
                {
                    StreamReader reader = new StreamReader(responseStream, Encoding.GetEncoding("utf-8"));
                    String errorText = reader.ReadToEnd();
                    // log errorText
                }
                throw;
            }
        }

    }


    public class Buy
    {
        public int listings { get; set; }
        public int unit_price { get; set; }
        public int quantity { get; set; }
    }

    public class Sell
    {
        public int listings { get; set; }
        public int unit_price { get; set; }
        public int quantity { get; set; }
    }

    public class RootObject
    {
        public int id { get; set; }
        public List<Buy> buys { get; set; }
        public List<Sell> sells { get; set; }
    }

Form1.cs

 private void button1_Click(object sender, EventArgs e)
        {
            RootObject RootObject = new RootObject();
            api_Request api_Request = new api_Request();
            richTextBox1.Text = api_Request.GetApi("https://api.guildwars2.com/v2/commerce/listings").id.ToString();
        }

In this json is a single ID, so this works fine. https://api.guildwars2.com/v2/commerce/listings/19684

But when retrieving multiple ID's like here, it breaks. https://api.guildwars2.com/v2/commerce/listings

Upvotes: 2

Views: 10643

Answers (2)

Coder1409
Coder1409

Reputation: 498

Here is a simple solution to get listing of all id's it will a real amount of time though to go through all of them

  List<RootObject> rootobject = new List<RootObject>();
    using (var webclient = new WebClient())
    {
        var Ids = webclient.DownloadString(" https://api.guildwars2.com/v2/commerce/listings");
        foreach (var id in Ids.Substring(1, s.Length-2).Split(','))
        {
            string url = string.Format("{0}/{1}","https://api.guildwars2.com/v2/commerce/listings",id);
            var res = webclient.DownloadString(url);
            var jsonObject = JsonConvert.DeserializeObject<RootObject>(res);
            rootobject.Add(jsonObject);
        }


    }

Upvotes: 1

David Watts
David Watts

Reputation: 2289

The link that you provided for multiple ID's return an Array of Integers. I would suggest parsing this as such:

var ids = JsonConvert.DeserializeObject<int[]>(jsonReader.ToString())

As you have said, your code works for a single item, so you can use your existing code for each individual request which instead of hitting https://api.guildwars2.com/v2/commerce/listings, will be hitting https://api.guildwars2.com/v2/commerce/listings/{id} which you can use string.Format() to add where it is needed.

Seriously consider how many of the id's returned in the int[] you actually want to fetch the root object for though, as from what I can see that it returns, you will be making a hell of a lot of requests.

Upvotes: 0

Related Questions