使用json文件kotlin

时间:2018-10-15 16:53:19

标签: android json kotlin

我的JSON文件是

    {
  "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": "babysallright@gmail.com",
        "emails": [
          "babysallright@gmail.com"
        ],
        "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
  }
}

我在这里得到它

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
    }

但是它不起作用!我想获取事件数组中一部分的位置对象属性,然后获取位置对象中一部分的位置对象属性!

1 个答案:

答案 0 :(得分: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
}

尝试一下,我已经编辑了您的代码。