0

My JSON file is

    {
  "events": [
    {
      "id": "836655879846811",
      "name": "U.S. Girls at Baby's All Right",
      "type": "public",
      "coverPicture": "https://scontent.xx.fbcdn.net/v/t31.0-8/s720x720/24883312_1521878931228093_3223523563973203944_o.jpg?oh=9bc3e5c5d45e39c542b057b92df95243&oe=5AC0353F",
      "profilePicture": "https://scontent.xx.fbcdn.net/v/t1.0-0/c0.0.200.200/p200x200/24862268_1521878931228093_3223523563973203944_n.jpg?oh=23ec7dc943402ec7e0137f2d17f27719&oe=5AC246F8",
      "description": "Friday, April 13th @ Baby's All Right\n\nAdHoc Presents\n\nU.S. Girls\n\nTickets:  http://ticketf.ly/2j7AegO\n\n| Baby's All Right |\n146 Broadway @ Bedford Ave | Williamsburg, Brooklyn \nJMZ-Marcy, L-Bedford, G-Broadway | 8pm | $12 | 21+\n\nCheck out our calendar and sign up for our mailing list http://adhocpresents.com/",
      "startTime": "2018-04-13T20:00:00.0400",
      "endTime": "2018-04-13T23:00:00.000",
      "timeFromNow": 9982924,
      "isCancelled": false,
      "category": "MUSIC_EVENT",
      "attending_count": "4356",
      "ticketing": {
        "ticket_uri": "http://ticketf.ly/2j7AegO"
      },
      "place": {
        "id": "460616340718401",
        "name": "Baby's All Right",
        "location": {
          "city": "Brooklyn",
          "country": "United States",
          "latitude": 40.71012,
          "longitude": -73.96348,
          "state": "NY",
          "street": "146 Broadway",
          "zip": "11211"
        }
      },
      "distances": {
        "venue": 89,
        "event": 89
      },
      "venue": {
        "id": "460616340718401",
        "name": "Baby's All Right",
        "about": "[email protected]",
        "emails": [
          "[email protected]"
        ],
        "coverPicture": "https://scontent.xx.fbcdn.net/v/t31.0-8/s720x720/20507438_1418517768261582_7945740169309872258_o.jpg?oh=24280a4732605e140c227db955c8d5e0&oe=5AC6B878",
        "profilePicture": "https://scontent.xx.fbcdn.net/v/t1.0-1/p200x200/1480734_642185745894792_5820988503650852577_n.png?oh=c6e72b8a5645644e7dd3eb3d2161329f&oe=5AC0CD2D",
        "category": "Bar",
        "categoryList": [
          "Bar",
          "Breakfast & Brunch Restaurant",
          "Dance & Night Club"
        ],
        "location": {
          "city": "Brooklyn",
          "country": "United States",
          "latitude": 40.71012,
          "longitude": -73.96348,
          "state": "NY",
          "street": "146 Broadway",
          "zip": "11211"
        }
      }
    }
  ],
  "metadata": {
    "venues": 100,
    "venuesWithEvents": 2,
    "events": 25
  }
}

and i am getting it in here

val eventList = ArrayList<Events>()

        try {
            // Load data
            val jsonString = loadJsonFromAsset(filename, context)
            val json = JSONObject(jsonString)
            val events = json.getJSONArray("events")
            val placeObj =json.optJSONObject("place")
            val locationObj = json.getJSONObject("location")

            (0 until events.length()).mapTo(destination = eventList) {
                Events(
                        events.getJSONObject(it).getString("id"),
                        events.getJSONObject(it).getString("name"),
                        events.getJSONObject(it).getString("attending_count"),
                        events.getJSONObject(it).getString("coverPicture"),
                        events.getJSONObject(it).getString("description"),
                        events.getJSONObject(it).getString("endTime"),
                        events.getJSONObject(it).getString("startTime"),
                        events.getJSONObject(it).getString("type"),
                        events.getJSONObject(it).getString("profilePicture"),
                        Place(
                                events.getJSONObject(it).getString("id"),
                                events.getJSONObject(it).getString("name"),
                                Location(
                                        events.getJSONObject(it).getString("city"),
                                     //   events.getJSONObject(it).getString("city_id").toInt(),
                                     //   events.getJSONObject(it).getString("name"),
                                        events.getJSONObject(it).getString("country"),
                                      //  events.getJSONObject(it).getString("country_code"),
                                        events.getJSONObject(it).getString("latitude").toFloat(),
                                        events.getJSONObject(it).getString("longitude").toFloat(),
                                      //  events.getJSONObject(it).getString("region"),
                                       // events.getJSONObject(it).getString("region_id").toInt(),
                                        events.getJSONObject(it).getString("state"),
                                        events.getJSONObject(it).getString("street"),
                                        events.getJSONObject(it).getString("zip")
                                )
                        )
                )
            }
        } catch (e: JSONException) {
            e.printStackTrace()
        }

        return eventList
    }

But it does not work ! i want to get the place object attributes which are part of events array and then location object attributes that are part of place object !

6
  • val events = json.getJSONArray("events") val placeObj =events.optJSONObject("place") val locationObj = placeObj.getJSONObject("location") Try this let me tell did it worked ? Commented Oct 15, 2018 at 16:57
  • This is a huge Json to parse. Instead, convert it to GSON pojo then it will be easy to parse. Commented Oct 15, 2018 at 16:58
  • @Mohsen what is GSON pojo ? tell me in detail ? Commented Oct 15, 2018 at 17:00
  • @SyedHamzaHassan okay i do it and will let you know ! Commented Oct 15, 2018 at 17:01
  • Go to jsonschema2pojo and paste the current json then check Annotation style: Gson and paste the code (POJO class) in your project. After that, it can be done by at most 3-5 lines of codes and no need for those too many lines. Commented Oct 15, 2018 at 17:04

1 Answer 1

1
val eventList = ArrayList<Events>()

    try {
        // Load data
        val jsonString = loadJsonFromAsset(filename, context)
        val json = JSONObject(jsonString)
        val events = json.getJSONArray("events")
        //val placeObj =events.optJSONObject("place")
        //val locationObj = placeObj.getJSONObject("location")

        (0 until events.length()).mapTo(destination = eventList) {
            Events(
                    val placeObj =events.optJSONObject(it).getJSONObject("place") 
                    val locationObj = placeObj.getJSONObject("location")

                    events.getJSONObject(it).getString("id"),
                    events.getJSONObject(it).getString("name"),
                    events.getJSONObject(it).getString("attending_count"),
                    events.getJSONObject(it).getString("coverPicture"),
                    events.getJSONObject(it).getString("description"),
                    events.getJSONObject(it).getString("endTime"),
                    events.getJSONObject(it).getString("startTime"),
                    events.getJSONObject(it).getString("type"),
                    events.getJSONObject(it).getString("profilePicture"),
                    Place(
                            placeObj.getString("id"),
                            placeObj.getString("name"),
                            Location(
                                    locationObj.getString("city"),
                                 //   locationObj.getString("city_id").toInt(),
                                 //   locationObj.getString("name"),
                                    locationObj.getString("country"),
                                  //  locationObj.getString("country_code"),
                                    locationObj.getString("latitude").toFloat(),
                                    locationObj.getString("longitude").toFloat(),
                                  //  locationObj.getString("region"),
                                   // locationObj.getString("region_id").toInt(),
                                    locationObj.getString("state"),
                                    locationObj.getString("street"),
                                    locationObj.getString("zip")
                            )
                    )
            )
        }
    } catch (e: JSONException) {
        e.printStackTrace()
    }

    return eventList
}

Try this, I have edited your code.

Sign up to request clarification or add additional context in comments.

4 Comments

it doesn't work ... only the line "val placeObj =events.optJSONObject("place")" gives error saying "type mismatched require int found string"
with some edit your code works fine ... just place the placeObj and locationObj in loop like this val placeObj =events.optJSONObject(it).getJSONObject("place") val locationObj = placeObj.getJSONObject("location")
Glad to here !!
you can edit your answer so others can see and get benefit from it !

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.