locked
Google Finance Json deseralization RRS feed

  • Question

  • hello,

    Please note this is solely for learning purpose.

    problem statement:

    URL: http://finance.google.com/finance/info?q=NSE%3AYESBANK

    Response: 

    // [ { "id": "5674106" ,"t" : "YESBANK" ,"e" : "NSE" ,"l" : "536.55" ,"l_fix" : "536.55" ,"l_cur" : "Rs.536.55" ,"s": "0" ,"ltt":"3:29PM GMT+5:30" ,"lt" : "Jul 28, 3:29PM GMT+5:30" ,"lt_dts" : "2014-07-28T15:29:59Z" ,"c" : "-2.55" ,"c_fix" : "-2.55" ,"cp" : "-0.47" ,"cp_fix" : "-0.47" ,"ccol" : "chr" ,"pcls_fix" : "539.1" } ]

    how do i get rid of "//" at the begnning of the Json response so that I can deserialize the response.

    Below is my attempt 

      public object GetResponse(string requestUri)
            {
                var request = (HttpWebRequest)WebRequest.Create(requestUri);
                request.Method = WebRequestMethods.Http.Get;
                request.Accept = "application/json";
                var json = ExtractJsonResponse(request.GetResponse());
                return json;
            }
    
            private GoogleQuoteResponse ExtractJsonResponse(WebResponse webResponse)
            {            
                DataContractJsonSerializer jsonSerializer = new DataContractJsonSerializer(typeof(GoogleQuoteResponse));
                object objResponse = jsonSerializer.ReadObject(webResponse.GetResponseStream());
                GoogleQuoteResponse jsonResponse = objResponse as GoogleQuoteResponse;
                return jsonResponse;
            }


    Response entity Class looks as below:

    using System;
    using System.Collections.Generic;
    
    using System.Runtime.Serialization;
    
    namespace StockQ.Models
    {
        [DataContract]
        internal class GoogleQuoteResponse
        {
            [DataMember]
            public string Id { get; set; }
            [DataMember]
            public string T { get; set; }
            [DataMember]
            public string E { get; set; }
            [DataMember]
            public string L { get; set; }
            [DataMember]
            public string LFix { get; set; }
            [DataMember]
            public string LCur { get; set; }
            [DataMember]
            public string S { get; set; }
            [DataMember]
            public string Ltt { get; set; }
            [DataMember]
            public string Lt { get; set; }
            [DataMember]
            public string LtDts { get; set; }
            [DataMember]
            public string C { get; set; }
            [DataMember]
            public string CFix { get; set; }
            [DataMember]
            public string Cp { get; set; }
            [DataMember]
            public string CpFix { get; set; }
            [DataMember]
            public string Ccol { get; set; }
            [DataMember]
            public string PclsFix { get; set; }
        }
    }
    

    When I try to de serialize it throws error "<" invalid character ...

    Thanks for guidance.

    Wednesday, July 30, 2014 12:14 AM

Answers

  • Hello,

    It seems to be that it would have the two “//” always, since I do not think we can post a feedback to that site to let them notice this scenario to remove it, we need to process the response text firstly as to remove the “//” and then deserialize the josn string to object. With your provided code, I modify it a little:

    Response entity Class is yours.

    GetResponse method, I modify it to return a josn string                :

    public static string GetResponse(string requestUri)
    
            {
    
                var request = (HttpWebRequest)WebRequest.Create(requestUri);
    
                request.Method = WebRequestMethods.Http.Get;
    
                request.Accept = "application/json";
    
                var ResponseStream = request.GetResponse().GetResponseStream();//ExtractJsonResponse(request.GetResponse());
    
                var objReader = new StreamReader(ResponseStream);
    
                var value = objReader.ReadToEnd();
    
                return value;
    
            }
    

    ExtractJsonResponse method, since the return josn from that link represents an array, so I use the

    List<GoogleQuoteResponse>  as the returned type and I use the JavaScriptSerializer:
    
    private static List<GoogleQuoteResponse> ExtractJsonResponse(string josn)
    
            {
    
                JavaScriptSerializer serializer1 = new JavaScriptSerializer();
    
                List<GoogleQuoteResponse> obje = serializer1.Deserialize<List<GoogleQuoteResponse>>(josn);
    
                return obje;
    
            }
    

    This is main method:

    var result = GetResponse("http://finance.google.com/finance/info?q=NSE%3AYESBANK").Trim();
    
    var josn = result.Substring(2, result.Length - 2);//remove “//”
    
    List<GoogleQuoteResponse> GoogleQuoteResponse = ExtractJsonResponse(josn);
    

    The result:

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by StSingh Wednesday, July 30, 2014 12:10 PM
    Wednesday, July 30, 2014 3:10 AM
    Moderator

All replies

  • Hello,

    It seems to be that it would have the two “//” always, since I do not think we can post a feedback to that site to let them notice this scenario to remove it, we need to process the response text firstly as to remove the “//” and then deserialize the josn string to object. With your provided code, I modify it a little:

    Response entity Class is yours.

    GetResponse method, I modify it to return a josn string                :

    public static string GetResponse(string requestUri)
    
            {
    
                var request = (HttpWebRequest)WebRequest.Create(requestUri);
    
                request.Method = WebRequestMethods.Http.Get;
    
                request.Accept = "application/json";
    
                var ResponseStream = request.GetResponse().GetResponseStream();//ExtractJsonResponse(request.GetResponse());
    
                var objReader = new StreamReader(ResponseStream);
    
                var value = objReader.ReadToEnd();
    
                return value;
    
            }
    

    ExtractJsonResponse method, since the return josn from that link represents an array, so I use the

    List<GoogleQuoteResponse>  as the returned type and I use the JavaScriptSerializer:
    
    private static List<GoogleQuoteResponse> ExtractJsonResponse(string josn)
    
            {
    
                JavaScriptSerializer serializer1 = new JavaScriptSerializer();
    
                List<GoogleQuoteResponse> obje = serializer1.Deserialize<List<GoogleQuoteResponse>>(josn);
    
                return obje;
    
            }
    

    This is main method:

    var result = GetResponse("http://finance.google.com/finance/info?q=NSE%3AYESBANK").Trim();
    
    var josn = result.Substring(2, result.Length - 2);//remove “//”
    
    List<GoogleQuoteResponse> GoogleQuoteResponse = ExtractJsonResponse(josn);
    

    The result:

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by StSingh Wednesday, July 30, 2014 12:10 PM
    Wednesday, July 30, 2014 3:10 AM
    Moderator
  • Thanks Fred... You are just awesome!
    Wednesday, July 30, 2014 12:11 PM