3

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();
        }
}
7
  • server.properties != emailServer.properties Commented Sep 18, 2019 at 6:22
  • Sorry man, I mispelled the properties file. Commented Sep 18, 2019 at 6:27
  • which json parser are you using? Commented Sep 18, 2019 at 6:27
  • Are you importing the right JSONObject class? Commented Sep 18, 2019 at 6:30
  • org.json.simple.parser.JSONParser Commented Sep 18, 2019 at 6:30

1 Answer 1

2

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);
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks man, Now i am getting. But there comes another issue, i am not getting the server1 and server2 details. int len = jadata.size(); for(int i=0; i<len; i++) { JSONArray jobject = (JSONArray)jsonObject.get("UserName"); System.out.println(jobject); }
Now again, your json array contain json objects so, you need to iterate over it to get Server1 and Server2 like this for (int i = 0; i <jadata.length(); i++) { JSONObject obj= jsonArray.get(i); String videoId=obj.get("server1"); }
Im getting exception like java.util.ArrayList can't cast to org.json.simple.JSONArray.. Please could you help me? why I'm getting this error.

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.