0

I'm trying to parse list of array from json object like below code,

File file = new File(HomePage.jsonFilePath);
if (file.exists()) {
    JSONParser parser = new JSONParser();
    try {
        Object obj = parser.parse(new FileReader(file));
        JSONObject jsonObject = (JSONObject) obj;
        System.out.println("Customer json for config customer " + jsonObject.toString());
        configuredBranch = (String) jsonObject.get("branch");
        configuredSystem = (String) jsonObject.get("system");
        expinContainerPath = (String) jsonObject.get("path");
        if (isEditable) {
            try {
                JSONArray customerArray = (JSONArray) jsonObject.get("customer_list");
                if (customerArray != null && customerArray.size() > 0) {
                    Iterator<String> iterator = customerArray.iterator();
                    while (iterator.hasNext()) {
                        System.out.println("customer gson" + iterator.next());
                        customerList.add(iterator.next());
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (ParseException e) {
        e.printStackTrace();
    }
}

And the console shows the below json when executing this line System.out.println("Customer json for config customer "+jsonObject.toString());

{"path":"C:\\Users\\Documents","system":"Exp","customer_list":["test","test1"],"branch":"LRD"}

But while iterating and printing like below System.out.println("customer gson"+iterator.next()); Its always prints test i.e first item in customer_list. I want to display all items in "customer_list". Could you please suggest me any idea to do this? thanks in advance.

0

1 Answer 1

1

You call the iterator's next() method twice inside the while loop, call it only once instead. So, rewrite your code:

while (iterator.hasNext()) {
    String customer = iterator.next();
    System.out.println("Customer " + customer);
    customerList.add(customer);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.