@@ -78,8 +78,8 @@ public static void getData(StringBuilder content) {
7878 System .out .println (phoneticObj );
7979 String text = (String ) phoneticObj .get ("text" );//phonetic Text
8080 System .out .println (text );
81- String audio = (String ) phoneticObj .get ("audio" );//audio String
82- runAudio (audio );
81+ // String audio = (String) phoneticObj.get("audio");//audio String
82+ // runAudio(audio);l
8383
8484 //Meanings
8585 JSONArray meanings = (JSONArray ) jsonObj .get ("meanings" );
@@ -95,22 +95,58 @@ public static void getData(StringBuilder content) {
9595 }
9696 //
9797 //Index One
98+ JSONObject meaningsIndexOne = (JSONObject ) meanings .get (1 );
99+ String partOfSpeech1 = (String ) meaningsIndexOne .get ("partOfSpeech" );
100+ System .out .println (partOfSpeech1 );
101+ JSONArray definitions1 = (JSONArray ) meaningsIndexOne .get ("definitions" );
102+ for (int i = 0 ; i < definitions1 .size () ; i ++){
103+ JSONObject defObj = (JSONObject ) definitions1 .get (i );
104+ String defString = (String ) defObj .get ("definition" );
105+ System .out .println ("Definition " + (i +1 ) + " : " + defString );
106+ }
98107
99108
100109 } catch (ParseException e ) {
101110 throw new RuntimeException (e );
102111 }
103112 }
113+
114+
115+ //Get Audio
116+ public static void getAudio (StringBuilder content ){
117+ try {
118+ JSONParser parser = new JSONParser ();
119+ JSONArray jsonArray = null ;
120+ jsonArray = (JSONArray ) parser .parse (content .toString ());
121+
122+ JSONObject jsonObj = (JSONObject ) jsonArray .get (0 );
123+ JSONArray phonetics = (JSONArray ) jsonObj .get ("phonetics" );
124+ for (int i = 0 ; i < phonetics .size () ; i ++){
125+ JSONObject phoneticObj = (JSONObject ) phonetics .get (i );
126+ String audio = null ;
127+ if (phoneticObj .containsKey ("audio" )){
128+ audio = (String ) phoneticObj .get ("audio" );
129+ }
130+ if (audio != null ){
131+ runAudio (audio );
132+ }
133+ else {
134+ System .out .println ("Audio not found for this word" );
135+ }
136+ }
137+ } catch (ParseException e ) {
138+ throw new RuntimeException (e );
139+ }
140+ }
141+
104142 public static void runAudio (String audio ){
105143 try {
106144 URL audioURL = new URL (audio );
107145 InputStream inputStream = audioURL .openStream ();
108146 BufferedInputStream bufferedInputStream = new BufferedInputStream (inputStream );
109147 Player player = new Player (bufferedInputStream );
110148 player .play ();
111- } catch (IOException e ) {
112- throw new RuntimeException (e );
113- } catch (JavaLayerException e ) {
149+ } catch (IOException | JavaLayerException e ) {
114150 throw new RuntimeException (e );
115151 }
116152
0 commit comments