如何在不使用Gson的情况下将JSON转换为数组

时间:2022-09-19 21:35:01

I have this JSON object:

我有一个JSON对象:

{"home_device_name":"light","light_status":[{"id_light":"1","status":"1"},{"id_light":"2","status":"0"}]}

I read it as a JSON object but I can't access "light_status", I want to convert it to an array to be able to read it.

我将它作为一个JSON对象读取,但是我不能访问“light_status”,我想将它转换为一个数组以读取它。

2 个解决方案

#1


1  

Use following code :

使用以下代码:

    String str = "{\"home_device_name\":\"light\",\"light_status\":[{\"id_light\":\"1\",\"status\":\"1\"},{\"id_light\":\"2\",\"status\":\"0\"}]}";

    try {
        JSONObject jsonObject = new JSONObject(str);

        String home_device_name = jsonObject.getString("home_device_name");

        JSONArray jsonArray = jsonObject.getJSONArray("light_status");

        for (int i = 0; i < jsonArray.length(); i++) {
            String id_light = jsonArray.getJSONObject(i).getString("id_light");
            String status = jsonArray.getJSONObject(i).getString("status");

            Log.d("Value", "Pos = " + i + " id_light = " + id_light + " status = " + status);
        }


    } catch (JSONException e) {
        e.printStackTrace();
    }

#2


-1  

First add below model into your project

首先将下面的模型添加到项目中

    class LightStatus {

    var idLight: String? = null
    var status: String? = null

}

Now You can use following code for getting light array

现在,您可以使用以下代码来获取光阵列。

    fun getLightArray() :ArrayList<LightStatus>{
    val jsonString = "{\"home_device_name\":\"light\",\"light_status\":[{\"id_light\":\"1\",\"status\":\"1\"},{\"id_light\":\"2\",\"status\":\"0\"}]}";
    val jsonObject=JSONObject(jsonString)
    val jsonArray =jsonObject.getJSONArray("light_status")
    val lightArray =ArrayList<LightStatus>()

    for (i in 0..jsonArray.length()-1){
        val lightStatus=LightStatus()
        lightStatus.idLight=jsonArray.getJSONObject(i).getString("id_light")
        lightStatus.status=jsonArray.getJSONObject(i).getString("status")
        lightArray.add(lightStatus)
    }
    return lightArray
}

#1


1  

Use following code :

使用以下代码:

    String str = "{\"home_device_name\":\"light\",\"light_status\":[{\"id_light\":\"1\",\"status\":\"1\"},{\"id_light\":\"2\",\"status\":\"0\"}]}";

    try {
        JSONObject jsonObject = new JSONObject(str);

        String home_device_name = jsonObject.getString("home_device_name");

        JSONArray jsonArray = jsonObject.getJSONArray("light_status");

        for (int i = 0; i < jsonArray.length(); i++) {
            String id_light = jsonArray.getJSONObject(i).getString("id_light");
            String status = jsonArray.getJSONObject(i).getString("status");

            Log.d("Value", "Pos = " + i + " id_light = " + id_light + " status = " + status);
        }


    } catch (JSONException e) {
        e.printStackTrace();
    }

#2


-1  

First add below model into your project

首先将下面的模型添加到项目中

    class LightStatus {

    var idLight: String? = null
    var status: String? = null

}

Now You can use following code for getting light array

现在,您可以使用以下代码来获取光阵列。

    fun getLightArray() :ArrayList<LightStatus>{
    val jsonString = "{\"home_device_name\":\"light\",\"light_status\":[{\"id_light\":\"1\",\"status\":\"1\"},{\"id_light\":\"2\",\"status\":\"0\"}]}";
    val jsonObject=JSONObject(jsonString)
    val jsonArray =jsonObject.getJSONArray("light_status")
    val lightArray =ArrayList<LightStatus>()

    for (i in 0..jsonArray.length()-1){
        val lightStatus=LightStatus()
        lightStatus.idLight=jsonArray.getJSONObject(i).getString("id_light")
        lightStatus.status=jsonArray.getJSONObject(i).getString("status")
        lightArray.add(lightStatus)
    }
    return lightArray
}