遍历JSON数据

时间:2019-12-23 07:05:21

标签: json kotlin

我正在用php生成JSON输出,我需要用它来填充用kotlin编写的android应用程序中的折线图。

JSON数据是:

[{"reading_temperature":"14","hour":"01"},{"reading_temperature":"14","hour":"02"},{"reading_temperature":"14","hour":"03"},{"reading_temperature":"14","hour":"04"},{"reading_temperature":"14","hour":"05"},{"reading_temperature":"14","hour":"06"},{"reading_temperature":"14","hour":"07"},{"reading_temperature":"14","hour":"08"},{"reading_temperature":"14","hour":"09"},{"reading_temperature":"14","hour":"10"},{"reading_temperature":"14","hour":"11"},{"reading_temperature":"14","hour":"12"},{"reading_temperature":"14","hour":"13"},{"reading_temperature":"14","hour":"14"},{"reading_temperature":"14","hour":"15"},{"reading_temperature":"14","hour":"16"},{"reading_temperature":"14","hour":"17"},{"reading_temperature":"14","hour":"18"},{"reading_temperature":"14","hour":"19"},{"reading_temperature":"14","hour":"20"},{"reading_temperature":"14","hour":"21"},{"reading_temperature":"14","hour":"22"},{"reading_temperature":"14","hour":"23"}]

这是我获取JSON的代码部分:

        override fun doInBackground(vararg params: String?): String? {
        var response:String?
        try{
            response = URL("https://127.0.0.1/weatherStation/temperatureDaily.php").readText(
                    Charsets.UTF_8
            )
        }catch (e: Exception){
            response = null
        }
        return response
    }

    override fun onPostExecute(result: String?) {
        super.onPostExecute(result)
        try {
            val jsonObj = JSONObject(result)

        } catch (e: Exception) {
        }
    }

现在我需要使用该数据填充折线图,这是我需要与JSON交换的硬编码数据

private fun setupLineChartDataTemperatura() {
val yVals = ArrayList<Entry>()

//This next part i need to dynamically generate from JSON, 
//first float value and last string value should be `"hour":"xx"`
// and second float value should be `[{"reading_temperature":"xx"`

yVals.add(Entry(0f, 4f, "0"))
yVals.add(Entry(1f, 5f, "1"))
yVals.add(Entry(1.5f, 4f, "1.5"))
yVals.add(Entry(2f, 5f, "2"))
yVals.add(Entry(3f, 3f, "3"))
yVals.add(Entry(4f, 2f, "4"))

val set1: LineDataSet
set1 = LineDataSet(yVals, "Temperatura")

val dataSets = ArrayList<ILineDataSet>()
dataSets.add(set1)
val data = LineData(dataSets)

chart1.setData(data)
chart1.description.isEnabled = false
chart1.legend.isEnabled = true
chart1.setPinchZoom(false)
chart1.xAxis.enableGridDashedLine(5f, 5f, 0f)
chart1.axisRight.enableGridDashedLine(5f, 5f, 0f)
chart1.axisLeft.enableGridDashedLine(5f, 5f, 0f)
chart1.setDrawGridBackground(true)
chart1.xAxis.labelCount = 11
chart1.xAxis.position = XAxis.XAxisPosition.BOTTOM
chart1.setTouchEnabled(true)
chart1.invalidate()
}

1 个答案:

答案 0 :(得分:0)

我设法做到了:

   val jsonTemperatureData = JSONArray(result)
            for (i in 0 until jsonTemperatureData.length()) {
                val item = jsonTemperatureData.getJSONObject(i)
                val reading_temperature = item.getString("reading_temperature")
                val hour = item.getString("hour")
                yVals.add(Entry(hour.toFloat(), reading_temperature.toFloat(), hour.toString()))
            }