【问题标题】:How to read JSON object in kotlin with Volle Android Studio?如何使用 Volle Android Studio 读取 kotlin 中的 JSON 对象?
【发布时间】:2022-12-05 03:04:08
【问题描述】:

我正在使用 volley 从 Google Places API 读取获取请求,我想从这个 JSON 输出中获取一些信息:

"results": [
{
"business_status": "OPERATIONAL",
  "geometry": {
    "location": {
      "lat": 25.7239497,
      "lng": -100.1915475
    },
    "viewport": {
      "northeast": {
        "lat": 25.7252242302915,
        "lng": -100.1902086197085
      },
      "southwest": {
        "lat": 25.7225262697085,
        "lng": -100.1929065802915
      }
    }
},
"icon": "https://maps.gstatic.com/mapfiles/place_api/icons/v1/png_71/restaurant-71.png",
"icon_background_color": "#FF9E67",
"icon_mask_base_uri": "https://maps.gstatic.com/mapfiles/place_api/icons/v2/restaurant_pinlet",
"name": "El Texanito",
"opening_hours": {},
"photos": [],
"place_id": "ChIJJdu3AjbqYoYRPJyEgQwBT-0",
"plus_code": {},
"price_level": 2,
"rating": 4,
"reference": "ChIJJdu3AjbqYoYRPJyEgQwBT-0",
"scope": "GOOGLE",
"types": [],
"user_ratings_total": 563,
"vicinity": "Boulevard Acapulco #141, Josefa Zozaya, Guadalupe"
},

我的科特林代码:

fun methodToGetInfo(){
    tbUsuarios?.removeAllViews()
    var queue = Volley.newRequestQueue(this)

    var url = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?fields=price_level&location=25.7299374%2C-100.2096866&radius=2500&type=restaurant&key=AIzaSyDV6aFItX960hrbAaI229-8iDa3xTZ-RXU"
    var myJsonObjectRequest = JsonObjectRequest(Request.Method.GET,url,null,
        Response.Listener {
                response ->  try{
            var myJsonArray = response.getJSONArray("results")
            for(i in 0 until myJsonArray.length()){
                var myJSONObject = myJsonArray.getJSONObject(i)
                val registro = LayoutInflater.from(this).inflate(R.layout.table_row_np,null,false)
                val colName = registro.findViewById<View>(R.id.columnaNombre) as TextView
                val colPrice = registro.findViewById<View>(R.id.columnaEmail) as TextView
                val colLatitude = registro.findViewById<View>(R.id.colEditar)
                val colBorrar = registro.findViewById<View>(R.id.colBorrar)

                colName.text= myJSONObject.getString("name")
                Log.d(TAG, "Nombre:  ${ myJSONObject.getString("name")}" )

                Log.d(TAG, "Rating:  ${ myJSONObject.getString("price_level")}" )
                colPrice.text=myJSONObject.getString("price_level")

                Log.d(TAG, "Latitude: ${myJSONObject.getString("location")}")




              

                tbUsuarios?.addView(registro)

            }

我可以轻松获取名称、价格评级、place_id 等信息,但是当我需要获取某些属性(例如 lat)中的数据时,我会收到错误消息。

我知道简单地搜索“lat”是错误的,因为我需要旅行“geometry”->“location”->“lat”

我想知道如何遍历这些属性并获取该信息

【问题讨论】:

    标签: json kotlin google-api android-volley get-request


    【解决方案1】:

    假设类型是org.json.JSONObject,你可以使用类似

    myJSONObject.getJSONObject("geometry").getJSONObject("location").getString("lat")
    

    如果不能保证这些字段存在,请使用safe calls

    myJSONObject.getJSONObject("geometry")?.getJSONObject("location")?.getString("lat")
    

    当然,如果您想从同一路径访问多个属性,也可以使用局部变量,例如

    val location = myJSONObject.getJSONObject("geometry").getJSONObject("location")
    val latitude = location.getString("lat")
    val longitude = location.getString("lng")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-16
      • 2019-11-19
      • 2019-01-20
      • 2020-09-19
      相关资源
      最近更新 更多