Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[Jsonapi.Root]' because the type requires a JSON array

asked 2021-11-19T08:47:57.49+00:00
Kalpana 246 Reputation points

Hi

I am trying to deserialize this json returned from this url -https://api.coindesk.com/v1/bpi/currentprice.json

However, I am getting the above error.

This is my code :

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace Jsonapi
{
    class Program
    {
        static void Main(string[] args)
        {
            HttpWebRequest WebReq = (HttpWebRequest)WebRequest.Create(string.Format("https://api.coindesk.com/v1/bpi/currentprice.json"));

            WebReq.Method = "GET";

            HttpWebResponse WebResp = (HttpWebResponse)WebReq.GetResponse();

            Console.WriteLine(WebResp.StatusCode);
            Console.WriteLine(WebResp.Server);

            string jsonString;
            using (Stream stream = WebResp.GetResponseStream())   //modified from your code since the using statement disposes the stream automatically when done
            {
                StreamReader reader = new StreamReader(stream, System.Text.Encoding.UTF8);
                jsonString = reader.ReadToEnd();
            }

            List<Root> friends = JsonConvert.DeserializeObject<List<Root>>(jsonString);

        }
    }
}

I am not sure how to read the json strings into the list format, could someone please guide me. I have generated the class for the json though.

{
    // Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(myJsonResponse); 
    public class Time
    {
        public string updated { get; set; }
        public DateTime updatedISO { get; set; }
        public string updateduk { get; set; }
    }

    public class USD
    {
        public string code { get; set; }
        public string symbol { get; set; }
        public string rate { get; set; }
        public string description { get; set; }
        public double rate_float { get; set; }
    }

    public class GBP
    {
        public string code { get; set; }
        public string symbol { get; set; }
        public string rate { get; set; }
        public string description { get; set; }
        public double rate_float { get; set; }
    }

    public class EUR
    {
        public string code { get; set; }
        public string symbol { get; set; }
        public string rate { get; set; }
        public string description { get; set; }
        public double rate_float { get; set; }
    }

    public class Bpi
    {
        public USD USD { get; set; }
        public GBP GBP { get; set; }
        public EUR EUR { get; set; }
    }`
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
6,910 questions
{count} votes

Accepted answer
  1. answered 2021-11-19T09:20:26.917+00:00
    Viorel 82,286 Reputation points

    Try this:

    Root friends = JsonConvert.DeserializeObject<Root>( jsonString );
    . . .
    public class Root
    {
       public Time time { get; set; }
       public string disclaimer { get; set; }
       public string chartName { get; set; }
       public Bpi bpi { get; set; }
    }
    
    No comments

0 additional answers

Sort by: Most helpful