我很难获得JSON数组以及如何从JSON响应中精确定位特定数组。 我必须从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=
获取所有图片网址所以我的结果json应该是这样的,因为我只需要为我的应用程序使用image src和alt。
{
"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"
}]
}
编辑
经过一些指导,我做了一些更改,我的代码看起来像这样。
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();}
}
}
但我仍然无法从我给出的网址获取数据。
答案 0 :(得分:0)
考虑到你使用的是android
提供的普通JSONObject类 JSONObject jObj= new JSONObject(response);
JSONArray jArray=jObj.getJSONArray("images");
然后使用for循环从该数组中获取数据
答案 1 :(得分:0)
假设您的回复是字符串
JSONObject object=new JSONObject(response);
JSONArray imageArray= object.getJSONObject("response").getJSONArray("images");
但如果你的回复已经是JSON格式,那么你只需要这样做
JSONArray imageArray= response.getJSONObject("response").getJSONArray("images");
答案 2 :(得分:0)
JSONObject jObj = new JSONObject(response); JSONArray jArray = jObj.getJSONArray(“images”);对于(I = 0;我
String image = object.getString(“src”);
//然后将其添加到lis
}