Search code examples
c#asp.net.netwcfdatacontract

DataContractJsonSerializer ReadObject return NULL value?


I have problem to deserialize this JSON provided by a REST Web Service. This is my suggestion to resolve the problem

class Program
{       
    static void Main(string[] args)
    {
        RestResponse r = readFromWeb("http://www.808.dk/?code-csharp-httpwebrequest");
        Console.ReadKey();
    }
    static RestResponse readFromWeb(string url)
    {
        HttpWebRequest request = HttpWebRequest.Create("http://services.groupkt.com/country/get/all") as HttpWebRequest;
        request.ContentType = "application/json";
        request.Method = "GET";
        HttpWebResponse response = request.GetResponse() as HttpWebResponse;
        Stream webStream = response.GetResponseStream();
        DataContractJsonSerializer json = new DataContractJsonSerializer(typeof(RestResponse));
        RestResponse resttresponse = (RestResponse)json.ReadObject(webStream);
        return resttresponse;
    }
}

And the DataContract class

[DataContract]
public class RestResponse
{
    [DataMember]
    public List<string> messages { get; set; }
    [DataMember]
    public List<City> result { get; set; }       
}
[DataContract]
public class City
{        
    [DataMember]
    public string name { get; set; }
    [DataMember]
    public string alpha2_code { get; set; }
    [DataMember]
    public string alpha3_code { get; set; }
}

Any suggestion? Cordially.


Solution

  • You still need Json object that hold RestResponse, take a look at this sample, it works:

      internal class Program
      {
        private static void Main(string[] args)
        {
          //var r = readFromWeb("http://www.808.dk/?code-csharp-httpwebrequest");
          var r = readFromWeb("http://services.groupkt.com/country/get/all");
          Trace.Write(r);
        }
    
        private static MyJson readFromWeb(string url)
        {
          var request = WebRequest.Create(url) as HttpWebRequest;
          request.ContentType = "application/json";
          request.Method = "GET";
          var response = request.GetResponse() as HttpWebResponse;
          var webStream = response.GetResponseStream();
          var json = new DataContractJsonSerializer(typeof (MyJson));
          var resttresponse = (MyJson) json.ReadObject(webStream);
          return resttresponse;
        }
      }
    
      [DataContract]
      public class MyJson
      {
        [DataMember(Name = "RestResponse")]
        public RestResponse RestResponse { get; set; }
      }
    
      [DataContract]
      public class RestResponse
      {
        [DataMember(Name = "messages")]
        public string[] messages { get; set; }
    
        [DataMember(Name = "result")]
        public City[] result { get; set; }
      }
    
      [DataContract]
      public class City
      {
        [DataMember(Name = "name")]
        public string name { get; set; }
    
        [DataMember(Name = "alpha2_code")]
        public string alpha2_code { get; set; }
    
        [DataMember(Name = "alpha3_code")]
        public string alpha3_code { get; set; }
      }