locked
Json serialization returns null RRS feed

  • Question

  • Hello I have this code:

    Trough HttpClient I am recieving this Json string: 

    {"group":3,"data":[{"count":1,"providerName":"BetaDigital","providerNo":12},{"count":139,"providerName":"Free to air","providerNo":1}]} var serializer = new DataContractJsonSerializer(typeof(GroupProvider)); var ms = new MemoryStream(Encoding.UTF8.GetBytes(jsonString)); var data = (GroupProvider) serializer.ReadObject(ms);

    Then I have this classes:

    [DataContract]
    public class GroupProvider 
    {
        public int Group { get; set; }
        public DataGroupProvider[] data { get; set; }
    }
    
    [DataContract]
    public class DataGroupProvider 
    {
    
        public int Count { get; set; }
        public string ProviderName { get; set; }
        public int ProviderNo { get; set; }
    }

    Problem is, that only the Group is filled and the DataGroupProvider is null..

    Where is the problem?


    • Edited by Petrtim Sunday, July 17, 2016 12:31 PM
    Sunday, July 17, 2016 12:27 PM

Answers

  • Hi,

    in the JSON string you are using providerNo, providerName with small letter "p"  and count "c" small letter and group "g" small, change them to be the same as in your class.

    {"group":3,"data":[{"count":1,"providerName":"BetaDigital","providerNo":12},{"count":139,"providerName":"Free to air","providerNo":1}]}

    while you are defining your properties in the class with Capital letters

    change your json to be 

    {"Group":3,"data":[{"Count":1,"ProviderName":"BetaDigital","ProviderNo":12},{"Count":139,"ProviderName":"Free to air","ProviderNo":1}]}


    Thanks, Mahmoud

    • Proposed as answer by Jayden Gu Wednesday, July 27, 2016 9:06 AM
    • Marked as answer by Barry Wang Thursday, July 28, 2016 1:51 AM
    Sunday, July 17, 2016 1:16 PM
  • In addition to Mahmoud's comments I prefer Json.Net for serializing and deserializing Json it is much quicker and has more features
    • Proposed as answer by Jayden Gu Wednesday, July 27, 2016 9:06 AM
    • Marked as answer by Barry Wang Thursday, July 28, 2016 1:52 AM
    Sunday, July 17, 2016 1:55 PM

All replies

  • Hi,

    in the JSON string you are using providerNo, providerName with small letter "p"  and count "c" small letter and group "g" small, change them to be the same as in your class.

    {"group":3,"data":[{"count":1,"providerName":"BetaDigital","providerNo":12},{"count":139,"providerName":"Free to air","providerNo":1}]}

    while you are defining your properties in the class with Capital letters

    change your json to be 

    {"Group":3,"data":[{"Count":1,"ProviderName":"BetaDigital","ProviderNo":12},{"Count":139,"ProviderName":"Free to air","ProviderNo":1}]}


    Thanks, Mahmoud

    • Proposed as answer by Jayden Gu Wednesday, July 27, 2016 9:06 AM
    • Marked as answer by Barry Wang Thursday, July 28, 2016 1:51 AM
    Sunday, July 17, 2016 1:16 PM
  • In addition to Mahmoud's comments I prefer Json.Net for serializing and deserializing Json it is much quicker and has more features
    • Proposed as answer by Jayden Gu Wednesday, July 27, 2016 9:06 AM
    • Marked as answer by Barry Wang Thursday, July 28, 2016 1:52 AM
    Sunday, July 17, 2016 1:55 PM