Jake2Finn
Jake2Finn

Reputation: 536

get a list of parsed json elements

I parsed a json string to the following object structure using gson:

data class Base (
        val expand: String,
        val startAt: Long,
        val maxResults: Long,
        val total: Long,
        val issues: List<Issue>
)

data class Issue (
        val expand: String,
        val id: String,
        val self: String,
        val key: String,
        val fields: Fields
)

data class Fields (
        val summary: String,
        val issuetype: Issuetype,
        val customfield10006: Long? = null,
        val created: String,
        val customfield11201: String? = null,
        val status: Status,
        val customfield10002: Customfield10002? = null,
        val customfield10003: String? = null
)

Everything works fine and also the object model is correct, because I can access each element of the object. However, I encountered the problem that I dont know how to get a list of all field-elements. Right now, I have only figured out how to access one item (by using an index and get()-function):

val baseObject = gson.fromJson(response, Base::class.java)
val fieldsList = baseObject.issues.get(0).fields

I actually want to have a list of all field elements and not just one. Is there a gson function allowing me to do that? I couldn't find anything about it in the gson documentation for java.

Upvotes: 0

Views: 82

Answers (1)

szymon_prz
szymon_prz

Reputation: 560

You don't have to look for some gson function when you've already created a baseObject. You just need to get from each issue it's fields and you can use a map function to achieve this, it will convert each issue to a new type so you can get issue fields there

val fieldFromAllIssues: List<Fields> = baseObject.issues.map { it.fields }

it in this context is a one issue. More explanation about it is here

Upvotes: 1

Related Questions