我有一个网址,例如www.example.com/movies/
,其中包含以下JSON数组:
[
{
"id": "alice",
"director": "Tim Burton",
"year": "2010",
"image":"alice",
"length": "108 min",
"stars": "Mia Wasikowska, Johnny Depp, Helena Bonham Carter",
"name": "Alice in Wonderland",
"description": "Nineteen-year-old Alice returns to the magical world from her childhood adventure, where she reunites with her old friends and learns of her true destiny: to end the Red Queen's reign of terror.",
"rating": "6.5",
"url": "http:\/\/ia.media-imdb.com\/images\/M\/MV5BMTMwNjAxMTc0Nl5BMl5BanBnXkFtZTcwODc3ODk5Mg@@._V1_SY317_CR0,0,214,317_AL_"
},
{
"id": "avator",
"director": "Cameron",
"year": "2009",
"image":"avatar",
"length": "162 min",
"stars": "Sam Worthington, Zoe Saldana, Sigourney Weaver",
"name": "Avatar",
"description": "A paraplegic Marine dispatched to the moon Pandora on a unique mission becomes torn between following his orders and protecting the world he feels is his home.",
"rating": "7.9",
"url": "http:\/\/ia.media-imdb.com\/images\/M\/MV5BMTYwOTEwNjAzMl5BMl5BanBnXkFtZTcwODc5MTUwMw@@._V1_SY317_CR0,0,214,317_AL_"
}]
我想从Android获取此JSON数组并将其转换为Hashmap。请帮帮我。
答案 0 :(得分:0)
您可以从网址
获取JSON响应 HttpClient client;
String _responseMain = "";
HttpResponse responseMain;
AsyncTask<String, String, String> _Task = new AsyncTask<String, String, String>() {
@Override
protected void onPreExecute() {
}
@Override
protected String doInBackground(String... arg0)
{
if (NetworkAvailablity.checkNetworkStatus(CurrentActivity.this))
{
try {
// Creating HTTP client
client = new DefaultHttpClient();
// Creating HTTP Post
HttpGet httpGet= new HttpGet("http://api.myjson.com/bins/44tys%22");
responseMain = client.execute(httpGet);
HttpEntity entitity = responseMain.getEntity();
_responseMain = EntityUtils.toString(entitity); // content will be consume only once
System.out.println("Response : "+_responseMain);
} catch (UnsupportedEncodingException e) {
// writing error to Log
e.printStackTrace();
}
catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
else
{
}
return null;
}
@Override
protected void onPostExecute(String result)
{
if (_responseMain!=null&&!_responseMain.equalsIgnoreCase("")) {
try {
JSONArray _jArr = new JSONArray("YOUR_JSON_STRING");
HashMap<String, String> pairs = new HashMap<String, String>();
if (_jArr.length()>0) {
for (int i = 0; i < _jArr.length(); i++) {
JSONObject _jObject = _jArr.getJSONObject(i);
String _id = _jObject.getString("id");
pairs.put("id", _id);
String _director = _jObject.getString("director");
pairs.put("director", _director);
String _year = _jObject.getString("year");
pairs.put("year", _year);
String _image = _jObject.getString("image");
pairs.put("image", _image);
String _length = _jObject.getString("length");
pairs.put("length", _length);
String _stars = _jObject.getString("stars");
pairs.put("stars", _stars);
String _name = _jObject.getString("name");
pairs.put("name", _name);
String _description = _jObject.getString("description");
pairs.put("description", _description);
String _rating= _jObject.getString("rating");
pairs.put("rating", _rating);
String _url = _jObject.getString("url");
pairs.put("url", _url);
}
}
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} else {
}
}
};
_Task.execute((String[]) null);