首页 > 解决方案 > Using JSON in a Java Program

问题描述

In a nutshell I'm trying to write some code so I am able to load a JSON file into my application and based on the current day/month, my program reads the correct JSON array and populates the remaining fields with those stored in the JSON array.

I have the following chunk of code:

        JSONArray jsonArray = new JSONArray(loadJSON());

        DateFormat dayOfMonth = new SimpleDateFormat("dd");
        String dayOfMonth2 = dayOfMonth.format(new Date());
        int result = Integer.parseInt(dayOfMonth2);

        DateFormat monthOfYear = new SimpleDateFormat("MM");
        String monthOfYear2 = monthOfYear.format(new Date());
        int result2 = Integer.parseInt(monthOfYear2);

        try {

            if (jsonArray.getInt(0) == result && jsonArray.getInt(1) == result2 ) {
                day = jsonArray.optString(0);
                month = jsonArray.optString(1);
                one = jsonArray.optString(2);
                two = jsonArray.optString(3);
                three = jsonArray.optString(4);
                four = jsonArray.optString(5);
                five = jsonArray.optString(6);
                six = jsonArray.optString(7);
                seven = jsonArray.optString(8);
            }
        } catch (Exception e) {
                System.out.print("Unable to find times for this day");
        }

So I'm storing the dayOfMonth in a variable based on the current date. I'm also storing monthOfYear in a variable based on what year of the month it is.

My JSON holds the following values:

["15","12","06:00","08:00","12:00","13:00","14:00","16:00","17:00"]
["16","11","06:26","08:03","12:09","13:46","14:17","16:05","17:42"]
["17","11","06:26","08:03","12:09","13:46","14:17","16:05","17:42"]
["18","11","06:26","08:03","12:09","13:46","14:17","16:05","17:42"]

The program loads in the first values with ease but whenever I move the position of the first JSON array, it doesn't populate the field with the values.

It seems the if statement is comparing only against the first array held in the .json file and if that's not the correct day/month it leaves the fields blank.

How can I modify this such that it checks every JSON array until it finds the corresponding day/month values and thereafter it stores these values in the string variables.

Load JSON:

public String loadJSON() {
        String json;
        try {
            InputStream is = getAssets().open("test.json");
            int size = is.available();
            byte[] buffer = new byte[size];
            is.read(buffer);
            is.close();
            json = new String(buffer, "UTF-8");
        } catch (IOException ex) {
            ex.printStackTrace();
            return null;
        }
        return json;
    }

标签: javaarraysjson

解决方案


好的,所以我认为在此之后JSONArray jsonArray = new JSONArray(loadJSON());您的 jsonArray 仅包含文件的第一行(因为该文件不是有效的 json)

我认为您应该将文件的行读取到 aList<String>然后JSONArray为循环内的每个元素调用构造函数(因为您的所有行都是有效的 Json,但不是整个文件)

我希望这有帮助。如果没有,我认为您应该提供更多代码,以便我可以在本地运行它


推荐阅读