kamikat / moshi-jsonapi

JSON API v1.0 Specification in Moshi.
MIT License
156 stars 34 forks source link

How can I get Included data? #46

Closed mirmousaviii closed 7 years ago

mirmousaviii commented 7 years ago

I use retrofit and I need get data from included. response:

{
    "data": {
        "type": "aaa",
        "id": 1,
        "attributes": {
            "username": "user1",
            "created_at": "2017-01-16T15:12:06+00:00",
            "updated_at": "2017-01-16T15:12:06+00:00"
        },
        "relationships": {
            "rr1": {
                "data": {
                    "type": "ccc",
                    "id": 2
                }
            }
        }
    },
    "included": [
        {
            "type": "ccc",
            "id": 2,
            "attributes": {
                "title": "title one",
                "description": "description one"
            },
            "relationships": {
                "rr22": {
                    "data": {
                        "type": "ddd",
                        "id": 2
                    }
                }
            }
        }
    ]
}

I can get Id and Type with response.body().getDocument().included.get(0).getId(), But How can I get attributes from included? for example title and description

Thank you.

mirmousaviii commented 7 years ago

@kamikat Can you help me?

kamikat commented 7 years ago

response.body().getDocument().included.get(0) gets just an instance of class with @JsonApi(type = "ccc") in your example. And you can access its fields (attributes/relationships) after cast to corresponding class.

BTW, Document.included is made unaccessible in 3.0. Could you please provide more detail about your practice?

mirmousaviii commented 7 years ago

It's resolved after cast to corresponding class. @kamikat Thank you for every help. Thank you.