-1

I'm having difficulty in getting JSON arrays and how exactly to target a specific array from a JSON response. i have to get all image url's from http://boilerpipe-web.appspot.com/extract?url=http%3A%2F%2Fwww.imdb.com%2Fmovies-in-theaters%2F%3Fref_%3Dnv_tp_inth_1&extractor=ArticleExtractor&output=json&extractImages=3&token=

so my resultant json should be like this,as i need only image src and alt for my application.

  {
 "images":
 [{
 "src":"https://images-na.ssl-images-amazon.com/images/M/MV5BMTQ5ODE4MTY2NV5BMl5BanBnXkFtZTgwMzM2NzEzMDI@._V1_UY209_C R0,0,140,209_AL_.jpg",
 "alt":"Collateral Beauty (2016) Poster"
 },
 {
 "src":"https://images-na.ssl-images-amazon.com/images/M/MV5BMjAwNDA1NjcwN15BMl5BanBnXkFtZTgwMDY0MDA2MDI@._V1_UY209_C R0,0,140,209_AL_.jpg",
 "alt":"A Kind of Murder (2016) Poster"
 },
 {
 "src":"https://images-na.ssl-images-amazon.com/images/M/MV5BMjA5MzgyNTIzMF5BMl5BanBnXkFtZTgwODg1OTY1MDI@._V1_UX140_C R0,0,140,209_AL_.jpg",
  "alt":"Solace (2015) Poster"
  },
  {
  "src":"https://images-na.ssl-images-amazon.com/images/M/MV5BOTg0Nzc1NjA0MV5BMl5BanBnXkFtZTgwNTcyNDQ0MDI@._V1_UX140_C R0,0,140,209_AL_.jpg",
 "alt":"Fences (2016) Poster"
  },
  {
  "src":"https://images-na.ssl-images-amazon.com/images/M/MV5BMTYxMjk0NDg4Ml5BMl5BanBnXkFtZTgwODcyNjA5OTE@._V1_UY209_CR0,0,140,209_AL_.jpg",
  "alt":"Manchester by the Sea (2016) Poster"
  },
  {
  "src":"https://images-na.ssl-images-amazon.com/images/M/MV5BMjI4MzU5NTExNF5BMl5BanBnXkFtZTgwNzY1MTEwMDI@._V1_UY209_CR0,0,140,209_AL_.jpg",
  "alt":"Moana (2016) Poster"
 }]
 }

EDIT

After some guidence i made some changes and my code looks like this.

public class Main2Activity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        TextView output = (TextView) findViewById(R.id.textView1);

        String strJson="http://boilerpipe-web.appspot.com/extract?url=http%3A%2F%2Fwww.imdb.com%2Fmovies-in-theaters%2F%3Fref_%3Dnv_tp_inth_1&extractor=KeepEverythingExtractor&output=img&extractImages=3&token=";
        String data = "";
        try {
            try {
                URL url=new URL(strJson);
                HttpURLConnection httpURLConnection=(HttpURLConnection)url.openConnection(); 
                httpURLConnection.connect();
                httpURLConnection.setDoInput(true);

                JSONObject  jsonRootObject = new JSONObject(strJson);
                JSONArray jsonArray = jsonRootObject.optJSONArray("images");
                for(int i=0; i < jsonArray.length(); i++){
                    JSONObject jsonObject = jsonArray.getJSONObject(i);
                    String name = jsonObject.optString("alt").toString();
                    String image = jsonObject.optString("src").toString();
                    data += "Node"+i+" : \n Name= "+ name+" \n Image="+image;
                }
                output.setText(data);
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

        } catch (JSONException e) {e.printStackTrace();}
    }
}

But still i am not able to get data from url i gave.

rocambille
  • 15,398
  • 12
  • 50
  • 68
Raj kumar
  • 27
  • 6
  • @Selvin can you give me a sample android code related to the url link i mentioned, as i am very new to JSON concepts, i just want to display the images in the given url in a listview in android. – Raj kumar Dec 15 '16 at 20:41

3 Answers3

0

Considering you are using normal JSONObject class provided by android

    JSONObject jObj= new JSONObject(response);
    JSONArray jArray=jObj.getJSONArray("images");

Then the for loop to get the data out of this array

Kaustubh Kadam
  • 182
  • 3
  • 13
0

Assuming your response is a String

JSONObject object=new JSONObject(response);
            JSONArray imageArray= object.getJSONObject("response").getJSONArray("images");

but if ur response is already in JSON format then you only need to do this

 JSONArray imageArray= response.getJSONObject("response").getJSONArray("images");
pipeur10
  • 63
  • 2
  • 6
0

JSONObject jObj= new JSONObject(response); JSONArray jArray=jObj.getJSONArray("images"); for(i=0;i

String image=object.getString("src");

//then add it to lis

}

ripan
  • 78
  • 8