1

So I have never had to define a class that the object name is going to vary. I have a collection of objects and the json result defines the key of the object (the name) as a string that is different for every object but really is should just be a property.

Live Json for inspection: Bungie Api Docs

Where "/User/GetBungieNetUserById/{id}/" should be a variable called endpoint. But their data uses it as a key for an object. Visual Studio defines a class like this when I use paste special.

C# Class:

public class Paths
    {
        [JsonProperty(PropertyName = "/User/GetBungieNetUserById/{id}/")]
        public UserGetbungienetuserbyidId UserGetBungieNetUserByIdid { get; set; }
    ...

When I am looking for something like this:

public class Paths
    {
        public Endpoint[] Endpoints { get; set; }
    }

    public class Endpoint
    {
        //Lots of properties
        public string route { get; set; }
    }

Simplified example:

{
    "openapi": "3.0.0",
    "paths": {
        "/User/GetBungieNetUserById/{id}/": {
            "summary": "User.GetBungieNetUserById",
            "description": "Loads a bungienet user by membership id.",
            "get": {
                "tags": [
                    "User"
                ],
                "description": "Loads a bungienet user by membership id.",
                "operationId": "User.GetBungieNetUserById",
                "parameters": [{
                    "name": "id",
                    "in": "path",
                    "description": "The requested Bungie.net membership id.",
                    "required": true,
                    "schema": {
                        "type": "integer",
                        "format": "int64"
                    }
                }],
                "responses": {
                    "200": {
                        "$ref": "#/components/responses/User.GeneralUser"
                    }
                }
            }
        },
// Many additional properties named after endpoints, e.g.:
        "/User/GetUserAliases/{id}/": {
        }
    }
}
dbc
  • 104,963
  • 20
  • 228
  • 340
Bailey Miller
  • 1,376
  • 2
  • 20
  • 36
  • That JSON doesn't look valid -- it's missing a beginning `{` for example. Can you please [edit] your question to include a sample of valid JSON? https://jsonlint.com/ can be used as a validator. – dbc Sep 07 '17 at 21:01
  • I am not going to copy the whole thing in, here is the actual json source. https://raw.githubusercontent.com/Bungie-net/api/master/openapi.json – Bailey Miller Sep 07 '17 at 21:02
  • Probably you want `public Dictionary { get; set; }` as shown in [How can I parse a JSON string that would cause illegal C# identifiers?](https://stackoverflow.com/a/24536564) or [Json.NET - format an array of objects with names](https://stackoverflow.com/q/39179252). – dbc Sep 07 '17 at 21:03
  • That looks very promising I need to run out really quick and when I come back I'll check that out. – Bailey Miller Sep 07 '17 at 21:04
  • In the future, you don't need to provide the actual JSON, but you do need to provide *valid* JSON that **exhibits the issue**. I usually insert `...[snip]...` into code to indicate I've snipped content out. –  Sep 07 '17 at 21:08
  • @BaileyMiller - I went ahead and marked it as a duplicate. If `public Dictionary { get; set; }` doesn't work for you, let us know what the problem is and I'll reopen the question and try to solve it for you. – dbc Sep 07 '17 at 21:13
  • It worked thank you very much. – Bailey Miller Sep 08 '17 at 12:43
  • @Amy is providing a link to the live json okay? Also my C# code it is very apparent that my code is simple snippets of a large code base. – Bailey Miller Sep 08 '17 at 12:44
  • No, because the live JSON can change at a future date, rendering your question useless. That's why code should be in the question. –  Sep 08 '17 at 13:10
  • For more information on that, see [Meta: Questions linking to external websites instead of showing code](https://meta.stackexchange.com/questions/80978/questions-linking-to-external-web-sites-instead-of-showing-code) –  Sep 08 '17 at 13:32

0 Answers0