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

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

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.
11,340 questions
{count} votes

Accepted answer
  1. Viorel 120.4K Reputation points
    2021-11-19T09:20:26.917+00:00

    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; }
    }
    
    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Sasikumar Santhakumar 0 Reputation points
    2023-02-10T09:52:23.9666667+00:00
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.