4

The following JSON object is what I'm receiving from server (get request). I need to get the coordinate values (lat, long)

{
    "loc": {
        "type": "Point",
        "coordinates": [
            -47.0487786,
            -22.9001656
        ]
    },
    "city": "New Jersey",
    "name": "John Doe",
    "_id": "5c7958b3e3234b3472d9917d"
}

I'm trying do this using the following Poko (Kotlin):

package com.zowye.API.Models

import com.google.gson.annotations.SerializedName


class Salao
    (
    @SerializedName("loc") var coordinate:  , // not sure about the type
    var city: String?,
    var name: String?
)

How can I parse it? Thanks.

1
  • Have you tried using Gson? Its a popular library that you can use to automatically map your json to a datamodel. Commented Mar 8, 2019 at 17:20

3 Answers 3

4

You should create a data class for "loc"

data class Salao(
        @SerializedName("loc")
        val location : Location,
        val city : String,
        val name : String,
        @SerializedName("_id")
        val id : String
    )

data class Location (
        val type : String,
        val coordinates : Array<Float>
    )
Sign up to request clarification or add additional context in comments.

1 Comment

IT WORKED! Thank you my friend!
2

Add one more class Location which represents the tested object type.

    package com.zowye.API.Models

    import com.google.gson.annotations.SerializedName


    class Location (
        var type: String?,
        var coordinates: Float[]?
    )

    class Salao
        (
        @SerializedName("loc") var coordinate: Location,
        var city: String?,
        var name: String?
    )

Comments

0

To build a class for this response, the best answer is:

data class UserAddress(

    @field:SerializedName("loc")
    val loc: Loc? = null,

    @field:SerializedName("city")
    val city: String? = null,

    @field:SerializedName("name")
    val name: String? = null,

     @field:SerializedName("_id")
     val id: String? = null
)

data class Loc(

     @field:SerializedName("coordinates")
     val coordinates: List<Float?>? = null,

     @field:SerializedName("type")
     val type: String? = null
)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.