0

I'm trying to translate the JSON object I get back from an API call to a C# class which I can in turn iterate over, however the JSON I can't seem to decipher the JSON correctly. JSON Object Sample:

  "status": "ok",
  "meta": {
    "count": 1,
    "hidden": null
  },
  "data": {
    "111111111": [
      {
        "ship_id": 4180588496
      },
      {
        "ship_id": 4284430032
      },
      {
        "ship_id": 3767482320
      }
     ]
   }
}

It's primarily the fact that under Data the Object has a different name for every call I make since it's the Player ID of the player I am requesting data on. In essence I want to be able to iterate over the data in the '1111111' class. I have no clue how to proceed anymore, I tried receiving a list of Interfaces that have the ship_id, I tried generating classes from JSON to C# but now I'm completely out of ideas. Any help would be appreciated. My current setup of receiving classes looks like this:

    public class WgShipRequest
    {
        public string Status { get; set; }

        public WgPlayerShip Data { get; set; }
    }

    public class WgPlayerShip
    {
        public IList<WgShipId> Ships { get; set; }
    }

    public class WgShipId
    {
        public long Ship_Id { get; set; }
    }
Brasidas
  • 3
  • 1
  • Make `Data` be a dictionary: `public Dictionary Data { get; set; }` as shown in [How can I parse a JSON string that would cause illegal C# identifiers?](https://stackoverflow.com/a/24536564/3744182) or [Create a strongly typed c# object from json object with ID as the name](https://stackoverflow.com/a/34213724/3744182). – dbc Apr 22 '20 at 17:55
  • I don't think it does since that would mean the key - 11111111- in this case would not be unique since it would occur with every value, the {ship_id : 123} – Brasidas Apr 22 '20 at 17:58
  • Ah sorry, it should be `Dictionary>`. Or `Dictionary>` if you prefer. The point is to use a `Dictionary` for an appropriate `T`. – dbc Apr 22 '20 at 18:03
  • Yeah that's it thanks man, I never knew a JSON object could in essence have a Dictionary in it's content. Learning every day. – Brasidas Apr 22 '20 at 18:05

1 Answers1

0

Your biggest issue is that your model doesn't match your json string. You can use a dictionary to represent your data and your model would look something like this:

        public void Deserialize()
        {
            var obj = JsonConvert.DeserializeObject<RootObject>(json);
        }

        private string json = @"{""status"": ""ok"",
  ""meta"": {
    ""count"": 1,
    ""hidden"": null
  },
  ""data"": {
    ""111111111"": [
      {
        ""ship_id"": 4180588496
      },
      {
        ""ship_id"": 4284430032
      },
      {
        ""ship_id"": 3767482320
      }
     ]
   }
}";

        public class RootObject
        {
            public string Status { get; set; }
            public MetaData Meta { get; set; }
            public IDictionary<string, IEnumerable<Ship>> Data { get; set; }
        }

        public class MetaData
        {
            public int Count { get; set; }
            public string Hidden { get; set; }
        }       

        public class Ship
        {
            public long Ship_Id { get; set; }
        }
JSteward
  • 6,833
  • 2
  • 21
  • 30