0

I have a JSON file in which the number of objects may vary, I want to read those objects using loop. But the order in which the data is read from JSON is different. I need data to read in same order as specified in JSON file.

Object obj = parser.parse(new FileReader("D:\\demo.json"));

              JSONObject jsonObject = (JSONObject) obj;
              for (Object key : jsonObject.keySet()) {
                //based on you key types
                String keyStr = (String)key;
                Object keyvalue = jsonObject.get(keyStr);

                //Print key and value
               System.out.println("key: "+ keyStr + " value: " + keyvalue);
                if(Driver.driver.findElement(By.name(keyStr)).isDisplayed() && Driver.driver.findElement(By.name(keyStr)).isEnabled())
                      Driver.driver.findElement(By.name(keyStr)).sendKeys((keyvalue.toString()));   
Atul
  • 857
  • 8
  • 14
  • What JSON lib do you use ? Probably this is for you: http://stackoverflow.com/questions/4515676/keep-the-order-of-the-json-keys-during-json-conversion-to-csv – PeterMmm Nov 09 '16 at 08:53
  • 4
    The order of properties of a JSON Object does not carry any meaning. This can be compared with the entries of a `Map`. If you need a defined order, use a JSON array to represent your data. – Henry Nov 09 '16 at 08:53

0 Answers0