I am not getting jsonArray in java?

Rockers :

I am trying to get the json data from properties file in java.

emailServer.properties

{ 
   "Servers":
   [ 
      { 
         "Name":"Server1",
         "UserName":"[email protected]",
         "Password":"something",
         "Port":"993",
         "FolderName":"Server1"
      },
      { 
         "Name":"Server2",
         "UserName":"[email protected]",
         "Password":"something",
         "Port":"993",
         "FolderName":"Server2"
      }
   ]
}

When i am trying to get servers array it is showing The method getJSONArray(String) is undefined for the type JSONObject. How to solve this? Here is my java code :-

public void configure()
    {
        JSONParser parser = new JSONParser();
        try
        {
            String propertyFileName = "emailServer.properties";
            InputStream inputStream = getClass().getClassLoader().getResourceAsStream(propertyFileName);

            JSONObject jsonObject = (JSONObject) parser.parse(new InputStreamReader(inputStream, "StandardCharsets.UTF_8"));
            System.out.println(jsonObject);
            JSONArray jadata = jsonObject.getJSONArray("Servers");
            System.out.println(jadata);

        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
}
VISHAL AGGARWAL :

Instead of using
jsonObject.getJSONArray("Servers"), you can use

JSONArray jadata =(JSONArray)jsonObject.get("Servers")

which may can solve your problem or if you still getting the issues then you can use google json library like Gson which you can find on maven and use below line:

yourjsonPojo[] data = gson.fromJson(jsonString, yourjsonPojo[].class);

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=164432&siteId=1