2

I am successfully deserializing Json files using JsonConvert.DeserializeObject() but I am having troubles with a fairly large json file with many structures. The problems is in the Stats subclasses which all have a category and abbreviation that are preceeded with @ and a text attribute preceeded by a #. The values are all comming out null.

Here is the class structure

using System;
using System.Collections.Generic;
using System.Text;

namespace SalaryCapData.ConsumeJson.Models.Daily
{

        public class Rootobject
        {
            public Dailyplayerstats dailyplayerstats { get; set; }
        }

        public class Dailyplayerstats
        {
            public string lastUpdatedOn { get; set; }
            public Playerstatsentry[] playerstatsentry { get; set; }
        }

        public class Playerstatsentry
        {
            public Player player { get; set; }
            public Team team { get; set; }
            public Stats stats { get; set; }
        }

        public class Player
        {
            public string ID { get; set; }
            public string LastName { get; set; }
            public string FirstName { get; set; }
            public string JerseyNumber { get; set; }
            public string Position { get; set; }
        }

        public class Team
        {
            public string ID { get; set; }
            public string City { get; set; }
            public string Name { get; set; }
            public string Abbreviation { get; set; }
        }

        public class Stats
        {
            public Atbats AtBats { get; set; }
            public Runs Runs { get; set; }
            public Hits Hits { get; set; }
            public Secondbasehits SecondBaseHits { get; set; }
            public Thirdbasehits ThirdBaseHits { get; set; }
            public Homeruns Homeruns { get; set; }
            public Earnedruns EarnedRuns { get; set; }
            public Unearnedruns UnearnedRuns { get; set; }
  .....
        public class Atbats
        {
            public string category { get; set; }
            public string abbreviation { get; set; }
            public string #text { get; set; }
        }

        public class Runs
        {
            public string category { get; set; }
            public string abbreviation { get; set; }
            public string text { get; set; }
        }

        public class Hits
        {
            public string category { get; set; }
            public string abbreviation { get; set; }
            public string text { get; set; }
        }

        public class Secondbasehits
        {
            public string category { get; set; }
            public string abbreviation { get; set; }
            public string text { get; set; }
        }


}

Here is a sample of the json {

"dailyplayerstats": {
    "lastUpdatedOn": "2018-02-22 8:10:18 AM",
    "playerstatsentry": [
      {
        "player": {
          "ID": "10437",
          "LastName": "Abreu",
          "FirstName": "Jose",
          "JerseyNumber": "79",
          "Position": "1B"
        },
        "team": {
          "ID": "119",
          "City": "Chicago",
          "Name": "White Sox",
          "Abbreviation": "CWS"
        },
        "stats": {
          "AtBats": {
            "@category": "Batting",
            "@abbreviation": "AB",
            "#text": "5"
          },
          "Runs": {
            "@category": "Batting",
            "@abbreviation": "R",
            "#text": "1"
          },
          "Hits": {
            "@category": "Batting",
            "@abbreviation": "H",
            "#text": "2"
          },

yadda, yadda, yadda.

Not sure if it the reason why the 'text' attribute is coimming back null but notice the '#'? Any ideas?

whackamadoodle3000
  • 6,684
  • 4
  • 27
  • 44
StephenG
  • 21
  • 3
  • 1
    Haven't seen a JSON file like this before. This almost looks like a weird XML to JSON thing with `@` denoting the attributes and `#text` being the inner text. – TyCobb Feb 22 '18 at 21:16
  • Disregard the '#text' in the sample code I posted. I was just screwing around - I tried everything else.. – StephenG Feb 22 '18 at 21:17
  • 2
    You can try tagging the properties with the JSON names. `[JsonProperty("#text")]` https://www.newtonsoft.com/json/help/html/JsonPropertyName.htm – TyCobb Feb 22 '18 at 21:21
  • Sorry, I should clarify. The #text in the class definitions was something I was trying and forgot to remove before posting. – StephenG Feb 22 '18 at 21:25
  • It doesn't matter. Your question specifically asks about the preceeding `#`. The whole point of that attribute is for you to put the name of the property from the JSON file. Whatever it is, fill it in and try ;) – TyCobb Feb 22 '18 at 21:27
  • Thank you TyCobb. I will try that. Would Notepad++ be a good editor for that? Actually, I guess I need to write a program to do that. I will be getting these files daily. I will try it. – StephenG Feb 22 '18 at 21:29
  • Now that I think of it, there must be a property in the Convert method that can ignore certain characters? – StephenG Feb 22 '18 at 21:33
  • That attribute goes onto your C# class property. – TyCobb Feb 22 '18 at 21:33
  • That JSON was clearly converted from XML using Json.NET's [Converting between JSON and XML](https://www.newtonsoft.com/json/help/html/ConvertingJSONandXML.htm) functionality. Why not deserialize the XML directly? But if you don't want to do that, see [How can I parse a JSON string that would cause illegal C# identifiers?](https://stackoverflow.com/a/24536739/3744182). You'll need `[JsonProperty("#text")]`. – dbc Feb 22 '18 at 22:55
  • Thanks to everyone for the support. I don't anything about an XML conversion. The sample that I posted here came directly from the feed. I certainly did not do any conversion. I will look at "How can I parse a JSON string that would cause illegal C# identifiers". – StephenG Feb 23 '18 at 15:30
  • Yippeeee!! Success!! :) Thank you everyone... – StephenG Feb 23 '18 at 15:41

0 Answers0