CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Feb 2011
    Posts
    47

    JSON Serialization

    Hey,
    Im building a mobile application in silverlight, so im using a stripped version of .NET 3.5 and VS 2010. Basically, i just make a HTTP request to a server that returns the details about the vechicle.
    Heres my class code:
    Code:
        [DataContract]
        public class Vehicle
        {
            [DataMember]
            string m_model;
            public string Model
            {
                get { return m_model; }                   //im aware of the get;set; syntax
                set { m_BodyStyle = m_model; }     // but i did it this way to try and debug my problem
            }
     //with a bunch more data members, not sure if you want to see them, cut them out for the sake of space. Its pretty long, but can post them if need be.
    And this is the Async request code:
    Code:
                Vehicle car = new Vehicle();
                //try
                {
                    HttpWebRequest request = (HttpWebRequest)asyncResult.AsyncState;
                    WebResponse response = (HttpWebResponse)request.EndGetResponse(asyncResult);
                    DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(Vehicle));
                    car = (Vehicle)ser.ReadObject(response.GetResponseStream());   
                }
    However, "car = (Vehicle)ser.ReadObject(response.GetResponseStream()); " gives an "System.InvalidCastException was unhandled" error. I assume the data coming from the server is in the wrong format, but i dont know how to fix the problem.
    Any extra code you want to see can be provided, no worries. Im just kinda stumped by this.
    Any input is greatly appreciated!
    Thanks,
    -M

  2. #2
    Join Date
    Jan 2007
    Posts
    491

    Re: JSON Serialization

    The first thing you need to do is to figure out where the problem is - is in your code, or is it in the input JSON content provided by the server?
    What is the JSON content returned by your request? Is it valid (you can use some JSON validator tool online to check that)?

  3. #3
    Join Date
    Feb 2011
    Posts
    47

    Re: JSON Serialization

    Well, the server has been built by somebody else, its basically been provided for me to access. I have no control over it, or how the information is sent. So I am to assume its 100% correct. And i have to make my application to work with it. Which leads me to believe that my code is wrong, but i dont know why?
    As for checking the JSON request, I dont know if its returning the correct data, i assume so? I think so. But im not sure how to check though, to be honest. Not alot of experience working with JSON.
    Thanks for your reply,
    M
    Last edited by Medic1de; April 12th, 2012 at 11:24 PM.

  4. #4
    Join Date
    Jan 2007
    Posts
    491

    Re: JSON Serialization

    Forget a second about JSON - read the content returned by your server(i.e., the content of the stream returned by response.GetResponseStream()). What is its content?

  5. #5
    Join Date
    Feb 2011
    Posts
    47

    Re: JSON Serialization

    Sorry its taken me a while to get back to you, had a busy weekend.
    Anyway, I cant seem to figure out how to look at the stream content. VS has no intellisense that shows it content, at least not that i can find. and I tried using System.Diagnostics.Debug.WriteLine(response.GetResponseStream()); which only returned "MS.Internal.InternalMemoryStream" which is obviously not much use.
    I assume the console will do the same. Care to enlighten me to an easier way?
    Thanks,
    M

  6. #6
    Join Date
    Jan 2007
    Posts
    491

    Re: JSON Serialization

    I mean you should read it - say, like this:
    Code:
    System.Diagnostics.Debug.WriteLine(response.GetResponseStream().ReadToEnd());

  7. #7
    Join Date
    Feb 2011
    Posts
    47

    Re: JSON Serialization

    Quote Originally Posted by Talikag View Post
    I mean you should read it - say, like this:
    Code:
    System.Diagnostics.Debug.WriteLine(response.GetResponseStream().ReadToEnd());
    Oh yeah, I tried that too, but due to silverlight for WP7 using a stripped version of .NET 3.5, .ReadtoEnd() isn't a method in the WebResponse class. Theres only basic stuff like read (which needs a byte array?) and write, flush etc. I cant seem to find anything that will let me just read the returned content :/
    Thanks for your help thus far though! Much appriciated!
    -M

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured