7 Replies - 970 Views - Last Post: 13 May 2015 - 02:56 PM Rate Topic: -----

#1 mouse88   User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 203
  • Joined: 27-July 10

JSON not deserializing

Posted 13 May 2015 - 12:38 PM

Hello all,

I ma using the following to send a http request which return a JSON string.

I am then trying to de-serialize the JSON string in to a matching class.

The JSON data is returned as expected but when I try to grab it from the class all the data is empty.

Can anyone see any issues in my code:

 public async static void GetPlayerDetails(string Username)
        {
            string url = string.Format(BUNGIE_BASE + "/Platform/Destiny/SearchDestinyPlayer/{0}/{1}", "2", Username);
            _client = new HttpClient();
            _client.DefaultRequestHeaders.Add("X-API-Key", BUNGIE_API_KEY);
            _client.DefaultRequestHeaders.Add("Accept", "application/json");
            _client.DefaultRequestHeaders.Add("x-csrf", xcsrfValue);

            HttpResponseMessage _response = await _client.GetAsync(url);
            //_response.EnsureSuccessStatusCode();
            string _responseBody = await _response.Content.ReadAsStringAsync();
            _playerDetails = JsonConvert.DeserializeObject<PlayerDetails>(_responseBody);
        }



 public class PlayerDetails
    {
        public string iconPath { get; set; }
        public int membershipType { get; set; }
        public string membershipId { get; set; }
        public string displayName { get; set; }
    }



Appreciate any help.

Regards
Matt

Is This A Good Question/Topic? 0
  • +

Replies To: JSON not deserializing

#2 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 6838
  • View blog
  • Posts: 23,215
  • Joined: 05-May 12

Re: JSON not deserializing

Posted 13 May 2015 - 12:40 PM

Without seeing a sample of the JSON data sent back, it's a bit hard to guess.
Was This Post Helpful? 0
  • +
  • -

#3 mouse88   User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 203
  • Joined: 27-July 10

Re: JSON not deserializing

Posted 13 May 2015 - 12:49 PM

View PostSkydiver, on 13 May 2015 - 12:40 PM, said:

Without seeing a sample of the JSON data sent back, it's a bit hard to guess.


Here's the returned data:

http://postimg.org/image/fr1n71eyt/
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 6838
  • View blog
  • Posts: 23,215
  • Joined: 05-May 12

Re: JSON not deserializing

Posted 13 May 2015 - 01:33 PM

Please post it as text, not an image. It's hard to try to parse and reformat the text is the picture to make it easier to line things up with properties.

On first glance it looks like the response you got back is associated with a Response class that contains a PlayerDetails class, but you are trying to parse it as a PlayerDetails class. Obviously that won't work.
Was This Post Helpful? 0
  • +
  • -

#5 rgfirefly24   User is offline

  • D.I.C Lover
  • member icon


Reputation: 462
  • View blog
  • Posts: 2,204
  • Joined: 07-April 08

Re: JSON not deserializing

Posted 13 May 2015 - 01:36 PM

your Json object that is returned is structured differently than your class.

I would use the two websites here when working with JSON they helped me Tremendously.

Json validator: json Lint
Json to C# class: Json 2 CSharp

They are not 100% foolproof, but they will work 95% of the time.

Here is what Json2CSharp shows:

public class Response
{
    public string iconPath { get; set; }
    public int membershipType { get; set; }
    public string membershipId { get; set; }
    public string displayName { get; set; }
}

public class MessageData
{
}

public class RootObject
{
    public List<Response> Response { get; set; }
    public int ErroCode { get; set; }
    public int ThrottleSeconds { get; set; }
    public string ErrorStatus { get; set; }
    public string Message { get; set; }
    public MessageData MessageData { get; set; }
}



Basically the initial { in your response denotes an Object which has an array of Responses, ErrorCode, ThrottleSeconds, ErrorStatus, MEssage, and MessageData.

Also if you want your class/Properties to be named something different than they are in the JSON string then you need to tell the JSON parser how they map using JSONProperty for Properites and custom converters.

This post has been edited by rgfirefly24: 13 May 2015 - 01:43 PM

Was This Post Helpful? 0
  • +
  • -

#6 mouse88   User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 203
  • Joined: 27-July 10

Re: JSON not deserializing

Posted 13 May 2015 - 01:53 PM

Thanks for the feedback all.

I re-created my class using the paste special feature and realized that the structure was incorrect.

I'm not too concerned about having different names than what the JSON data provides however wouldn't other JSON classes that I create conflict with these if they are in the same namespace.

Also how do I create a new instance of the JSON class for the data to be deserialized as it actually consists of 3 classes:

namespace Destiny_Manager.BungieData
{
    public class Rootobject
    {
        public Response[] Response { get; set; }
        public int ErrorCode { get; set; }
        public int ThrottleSeconds { get; set; }
        public string ErrorStatus { get; set; }
        public string Message { get; set; }
        public Messagedata MessageData { get; set; }
    }

    public class Messagedata
    {
    }

    public class Response
    {
        public string iconPath { get; set; }
        public int membershipType { get; set; }
        public string membershipId { get; set; }
        public string displayName { get; set; }
    }
}



Sorry if I am asking silly questions. It's been a while since I've done any software projects.

Regards
Matt
Was This Post Helpful? 0
  • +
  • -

#7 rgfirefly24   User is offline

  • D.I.C Lover
  • member icon


Reputation: 462
  • View blog
  • Posts: 2,204
  • Joined: 07-April 08

Re: JSON not deserializing

Posted 13 May 2015 - 02:17 PM

First, break each one up into it's own file.
Second you need to rename Root object to be meaningful to your code.

Now as far as deserializing, that's the beauty of JSON deserializers. They will handle all of the correlations for you. So In your RootObject you tell it that you have a Property of Type MessageData, and an Array of type Response[]. the Deserializer will then look in your code for these classes and automagically build a new instance of them. You only need to tell it where to start:

JsonConvert.Deserialize<RootObject>(_jsonData);

As far as it conflicting with other JSON Classes, as long as they are not named the same it doesn't matter. Because the deserializer will look at the property name as long as that matches your JSON Object it will parse it. If you want the property named something different you can use JSONProperty to tell the parser what it maps to.

EX:

Take two JSON Return sets. Each of them have a Student object, but the Data contained in them is completely different depending on which return you do. Let's assume return 1 is for UNI students and return 2 is for CommunityCollege.

So we have a Root Object for UNI.

public class UNI
{
    public UNIStudent[] Students { get; set;}
}  



and a root object for CommunityCollege

public class CommunityCollege
{
    public CCStudent[] Students { get; set; }
}



So as long as the JSON Objects has a node called Students it will map properly, but what if the JSON Property is named StudentFiles. We can then set a JSONProperty on the Class Property for mapping.


public class UNI
{ 
    [JsonProperty("StudentFiles")]
    public UNIStudent[] Students { get; set;}
}  


This post has been edited by rgfirefly24: 13 May 2015 - 02:17 PM

Was This Post Helpful? 0
  • +
  • -

#8 mouse88   User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 203
  • Joined: 27-July 10

Re: JSON not deserializing

Posted 13 May 2015 - 02:56 PM

Thank you.

Very detailed response and I have now have this working perfectly.

Regards
Matt
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1