簡體   English   中英

我想使用數組列出 JSON 字符串中的數據,但為什么我的應用程序崩潰了?

[英]I want to list data from JSON string using an array, but why my app crashes?

我想在 ListView 中列出來自 JSON 字符串的數據。 我將字符串存儲在一個數組中。 當我在JSONObject day = jsonArray.getJSONObject(0);行中寫i應用程序崩潰。

我做錯了什么?

String json_string1 = "{\"list\":[{\"dt\":1471255200,\"temp\":{\"day\":30.04,\"min\":17.35,\"max\":30.04},\"pressure\":1018.83,\"humidity\":35,\"weather\":[{\"id\":802,\"main\":\"Clouds\",\"de scription\":\"scattered clouds\",\"icon\":\"03d\"}],\"speed\":2.66,\"deg\":314,\"clouds\":48},{\"dt\":1471341600,\"temp\":{\"day\":29.48,\"min\":19.3,\"max\":29.95},\"pressure\":1018.63,\"humidity\":100,\"weather\":[{\"id\":501,\"main\":\"Rain\",\"des cription\":\"moderate rain\",\"icon\":\"10d\"}],\"speed\":2.46,\"deg\":28,\"clouds\":92,\"rain\":8.5},{\"dt\":1471428000,\"temp\":{\"day\":28.96,\"min\":17.84,\"m ax\":297.54},\"pressure\":1012.1,\"humidity\":95,\"weather\":[{\"id\":501,\"main\":\"Ra in\",\"description\":\"moderate rain\",\"icon\":\"10d\"}],\"speed\":2.27,\"deg\":118,\"clouds\":88,\"rain\":5.55},{\"dt\":1471514400,\"temp\":{\"day\":26.89,\"min\":17.84,\" max\":28.95},\"pressure\":1014.27,\"humidity\":83,\"weather\":[{\"id\":500,\"main\":\"R ain\",\"description\":\"light rain\",\"icon\":\"10d\"}],\"speed\":1.98,\"deg\":93,\"clouds\":20},{\"dt\":1471600800,\"temp\":{\"day\":30.04,\"min\":21.87,\"max\":30.04},\"pressure\":1014.65,\"humidity\":0,\"weather\":[{\"id\":500,\"main\":\"Rain\",\"descrip tion\":\"light rain\",\"icon\":\"10d\"}],\"speed\":1.66,\"deg\":194,\"clouds\":11,\"rain\":0.22},{\"dt\":1471687200,\"temp\":{\"day\":31.3,\"min\":22.56,\"max\":32.3},\"p ressure\":1018.6,\"humidity\":0,\"weather\":[{\"id\":500,\"main\":\"Rain\",\"descriptio n\":\"light rain\",\"icon\":\"10d\"}],\"speed\":1.69,\"deg\":180,\"clouds\":16,\"rain\":0.77},{\"dt\":1471773600,\"temp\":{\"day\":31.77,\"min\":21.67,\"max\":30.77},\"pressure\":1016.93,\"humidity\":0,\"weather\":[{\"id\":500,\"main\":\"Rain\",\"description\":\"light rain\",\"icon\":\"10d\"}],\"speed\":2.49,\"deg\":227,\"clouds\":14,\"rain\":2.2}]}";

JSONObject jsonObject;

String[] days_array = new String[7];

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    try {
        jsonObject = new JSONObject(json_string1);
        JSONArray jsonArray = jsonObject.optJSONArray("list");
        int lengthJsonArr = jsonArray.length();

        for (int i = 0; i < lengthJsonArr; i++) {

            JSONObject day = jsonArray.getJSONObject(0);

            JSONObject temp = day.getJSONObject("temp");
            int min = temp.getInt("min");
            int max = temp.getInt("max");

            JSONArray weather = day.getJSONArray("weather");

            JSONObject zero = weather.getJSONObject(0);
            String main = zero.getString("main");

            double speed = day.getDouble("speed");

            days_array[i] = max + "/" + min + ", " + main + ", Wind=" + speed;
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }

    ListView ListView = (ListView) findViewById(R.id.list_view);

    ArrayAdapter adapter = new ArrayAdapter<String>(this, R.layout.list_layout, R.id.item_name, days_array);

    ListView.setAdapter(adapter);
}

您有兩個錯誤的屬性名稱: "m ax"" max" 這個固定的 json 可以工作:

String json_string1 = "{\"list\":[{\"dt\":1471255200,\"temp\":{\"day\":30.04,\"min\":17.35,\"max\":30.04},\"pressure\":1018.83,\"humidity\":35,\"weather\":[{\"id\":802,\"main\":\"Clouds\",\"de scription\":\"scattered clouds\",\"icon\":\"03d\"}],\"speed\":2.66,\"deg\":314,\"clouds\":48},{\"dt\":1471341600,\"temp\":{\"day\":29.48,\"min\":19.3,\"max\":29.95},\"pressure\":1018.63,\"humidity\":100,\"weather\":[{\"id\":501,\"main\":\"Rain\",\"des cription\":\"moderate rain\",\"icon\":\"10d\"}],\"speed\":2.46,\"deg\":28,\"clouds\":92,\"rain\":8.5},{\"dt\":1471428000,\"temp\":{\"day\":28.96,\"min\":17.84,\"max\":297.54},\"pressure\":1012.1,\"humidity\":95,\"weather\":[{\"id\":501,\"main\":\"Ra in\",\"description\":\"moderate rain\",\"icon\":\"10d\"}],\"speed\":2.27,\"deg\":118,\"clouds\":88,\"rain\":5.55},{\"dt\":1471514400,\"temp\":{\"day\":26.89,\"min\":17.84,\"max\":28.95},\"pressure\":1014.27,\"humidity\":83,\"weather\":[{\"id\":500,\"main\":\"R ain\",\"description\":\"light rain\",\"icon\":\"10d\"}],\"speed\":1.98,\"deg\":93,\"clouds\":20},{\"dt\":1471600800,\"temp\":{\"day\":30.04,\"min\":21.87,\"max\":30.04},\"pressure\":1014.65,\"humidity\":0,\"weather\":[{\"id\":500,\"main\":\"Rain\",\"descrip tion\":\"light rain\",\"icon\":\"10d\"}],\"speed\":1.66,\"deg\":194,\"clouds\":11,\"rain\":0.22},{\"dt\":1471687200,\"temp\":{\"day\":31.3,\"min\":22.56,\"max\":32.3},\"p ressure\":1018.6,\"humidity\":0,\"weather\":[{\"id\":500,\"main\":\"Rain\",\"descriptio n\":\"light rain\",\"icon\":\"10d\"}],\"speed\":1.69,\"deg\":180,\"clouds\":16,\"rain\":0.77},{\"dt\":1471773600,\"temp\":{\"day\":31.77,\"min\":21.67,\"max\":30.77},\"pressure\":1016.93,\"humidity\":0,\"weather\":[{\"id\":500,\"main\":\"Rain\",\"description\":\"light rain\",\"icon\":\"10d\"}],\"speed\":2.49,\"deg\":227,\"clouds\":14,\"rain\":2.2}]}";

為了確保它不會再次發生,您可以使用在線工具(例如下一個https://jsoneditoronline.org/)來驗證您未來的 json,然后再認為您的代碼中有任何錯誤。

還有一件事,您還可以通過String jsonString1改進變量String json_string1的名稱,以保持 Java 的駝峰式大小寫約定

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM