none
Deserialze json object RRS feed

  • Question

  • Hi,


    Ik have a json object and i want to deserialize it. After some searching i made this but there is something wrong with it. Result is empty.



    private void Button2_Click(object sender, EventArgs e)
            {
    			//the json object:
                //{\"1\":{\"symbol\":\"BTC\",\"cap\":190543515710.2,\"change\":{\"hour\":-0.230294,\"day\":-0.524608},\"price\":10719.7326423,\"coinheat\":100,\"url\":\"https:\\/\\/chasing-coins.com\\/coin\\/BTC\"}
    
    			//fetch the data
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://chasing-coins.com/api/v1/top-coins/20");
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    
                StreamReader sr = new StreamReader(response.GetResponseStream());
                richTextBox1.Text = sr.ReadToEnd();  //Just to see the string
    
                sr.Close();
    
    			//deserialize json object
                string json = richTextBox1.Text;
    
                var result = JsonConvert.DeserializeObject<RootObject>(json);  //--> there is no result. (result = null)
    
                var capi = result.Coin.Select(p => p.BTC).ToList();  //p.cap , p.change --> all empty
    
            }
        }
    
        public class Coin
        {
            public string symbol { get; set; }
            public string BTC { get; set; }
            public string cap { get; set; }
            public string change { get; set; }
            public string hour { get; set; }
            public string day { get; set; }
            public string price { get; set; }
            public string coinheat { get; set; }
            public string url { get; set; }
            public string https { get; set; }
        }
    
        public class RootObject
        {
            public List<Coin> Coin { get; set; }
        }



    Sunday, June 30, 2019 6:14 PM

Answers

  • Hi HansvB69,

    Thank you for posting here.

    Based on your description, you want to deserialize json object.

    You could try the following code.

     public class Coin
        {
            public string symbol { get; set; }
            public double cap { get; set; }
            public Change change { get; set; }
            public double price { get; set; }
            public int coinheat { get; set; }
            public string url { get; set; }
        }
        public  class Change
        {
            public double hour { get; set; }
            public double day { get; set; }
          
        }
    
    private void Button1_Click(object sender, EventArgs e)
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://chasing-coins.com/api/v1/top-coins/20");
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    
                StreamReader sr = new StreamReader(response.GetResponseStream());
                richTextBox1.Text = sr.ReadToEnd();  //Just to see the string
    
                sr.Close();
                string m = richTextBox1.Text;
                string[] test = {"{\"symbol"};
                string[] n = m.Split(test,StringSplitOptions.None);
                for (int i = 1; i < n.Length; i++)
                {
                    //if(n[i])
                    n[i] = n[i].Insert(0, "{\"symbol");
                    if(i<n.Length-1)
                    {
                        n[i] = n[i].Remove(n[i].LastIndexOf(','));
                    }
                    else
                    {
                        n[i] = n[i].Remove(n[i].LastIndexOf('}'));
                    }
              
                }
                List<Coin> list = new List<Coin>();
                for (int i = 1; i < n.Length; i++)
                {
                    var result = JsonConvert.DeserializeObject<Coin>(n[i]);
                    list.Add(result);
                }
     
                var capi = list.Select(p => p.symbol).ToList();
    
            }

    Result:

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, July 1, 2019 7:47 AM
    Moderator

All replies

  • What I do. Maybe, you should switch over to Httpclient() that is the de facto.  

    I don't know what the Streamreader stuff is about when you should be able to use Quickwatch in debug mode and look at the context of any given object.

    public DtoCache GetCacheApi()
            {
                var dtocache = new DtoCache();
    
                using (var client = new HttpClient())
                {
                    var uri = new Uri("http://progmgmntcore2api.com/api/cache");
    
                    var response = client.GetAsync(uri).Result;
    
                    if (!response.IsSuccessStatusCode)
                        throw new Exception(response.ToString());
    
                    var responseContent = response.Content;
                    var responseString = responseContent.ReadAsStringAsync().Result;
    
                    var deserialized = JsonConvert.DeserializeObject<DtoCacheRoot>(responseString);
    
                    dtocache.ProjectTypes = deserialized.DtoCache.ProjectTypes;
                    dtocache.Durations = deserialized.DtoCache.Durations;
                    dtocache.Statuses = deserialized.DtoCache.Statuses;
                    dtocache.Resources = deserialized.DtoCache.Resources;
                }
    
                return dtocache;
            }

    amespace Entities
    {
        public class DtoCacheRoot
        {
            public DtoCache DtoCache { get; set; }
        }
    }
    using System.Collections.Generic;
    
    namespace Entities
    {
        public class DtoCache
        {
            public List<DtoProjectType> ProjectTypes { get; set; } = new List<DtoProjectType>();
            public List<DtoStatus> Statuses { get; set; } = new List<DtoStatus>();
            public List<DtoResource> Resources { get; set; } = new List<DtoResource>();
            public List<DtoDuration> Durations { get; set; } = new List<DtoDuration>();
        }
    }
    


    Sunday, June 30, 2019 7:30 PM
  • Try this too:

    public class Coin
    {
        public string symbol { get; set; }
        public float cap { get; set; }
        public Change change { get; set; }
        public float price { get; set; }
        public int coinheat { get; set; }
        public string url { get; set; }
    }
    
    public class Change
    {
        public float hour { get; set; }
        public float day { get; set; }
    }
    
    . . .
    
    var c = new WebClient( );
    string s = c.DownloadString( "https://chasing-coins.com/api/v1/top-coins/20" );
    Dictionary<int, Coin> result = JsonConvert.DeserializeObject<Dictionary<int, Coin>>( s );
    List<Coin> coins = result.OrderBy( p => p.Key ).Select( p => p.Value ).ToList( );
    
    

    Monday, July 1, 2019 5:59 AM
  • Hi HansvB69,

    Thank you for posting here.

    Based on your description, you want to deserialize json object.

    You could try the following code.

     public class Coin
        {
            public string symbol { get; set; }
            public double cap { get; set; }
            public Change change { get; set; }
            public double price { get; set; }
            public int coinheat { get; set; }
            public string url { get; set; }
        }
        public  class Change
        {
            public double hour { get; set; }
            public double day { get; set; }
          
        }
    
    private void Button1_Click(object sender, EventArgs e)
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://chasing-coins.com/api/v1/top-coins/20");
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    
                StreamReader sr = new StreamReader(response.GetResponseStream());
                richTextBox1.Text = sr.ReadToEnd();  //Just to see the string
    
                sr.Close();
                string m = richTextBox1.Text;
                string[] test = {"{\"symbol"};
                string[] n = m.Split(test,StringSplitOptions.None);
                for (int i = 1; i < n.Length; i++)
                {
                    //if(n[i])
                    n[i] = n[i].Insert(0, "{\"symbol");
                    if(i<n.Length-1)
                    {
                        n[i] = n[i].Remove(n[i].LastIndexOf(','));
                    }
                    else
                    {
                        n[i] = n[i].Remove(n[i].LastIndexOf('}'));
                    }
              
                }
                List<Coin> list = new List<Coin>();
                for (int i = 1; i < n.Length; i++)
                {
                    var result = JsonConvert.DeserializeObject<Coin>(n[i]);
                    list.Add(result);
                }
     
                var capi = list.Select(p => p.symbol).ToList();
    
            }

    Result:

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, July 1, 2019 7:47 AM
    Moderator
  • Thanks Jack,

    I see in the debugger the result. I can go further with this.

    Now i have to find an api which show the real time bitcoin price. (Central europe time).

    Saturday, July 6, 2019 5:09 PM