我需要解析这个Json字符串
{"results": { "result": [ { "cover": "http://ia.media-imdb.com/images/M/MV5BMjMyOTM4MDMxNV5BMl5BanBnXkFtZTcwNjIyNzExOA@@._V1._SX54_CR0,0,54,74_.jpg", "Title": "The Amazing SpIDer-Man", "year": "(2012", "director": "marc Webb", "rating": "7.3", "details": "http://www.imdb.com/Title/tt0948470/" }, { "cover": "http://i.media-imdb.com/images/SF1f0a42ee1aa08d477a576fbbf7562eed/realm/feature.gif", "Title": "The Amazing SpIDer-Man 2", "year": "(2014", "director": "N/A", "rating": "N/A", "details": "http://www.imdb.com/Title/tt1872181/" }, { "cover": "http://ia.media-imdb.com/images/M/MV5BMzk3MTE5MDU5NV5BMl5BanBnXkFtZTYwMjY3NTY3._V1._SX54_CR0,0,54,74_.jpg", "Title": "SpIDer-Man", "year": "(2002", "director": "Sam Raimi", "rating": "7.3", "details": "http://www.imdb.com/Title/tt0145487/" }, { "cover": "http://ia.media-imdb.com/images/M/MV5BODUwMDc5Mzc5M15BMl5BanBnXkFtZTcwNDgzOTY0MQ@@._V1._SX54_CR0,0,54,74_.jpg", "Title": "SpIDer-Man 3", "year": "(2007", "director": "Sam Raimi", "rating": "6.3", "details": "http://www.imdb.com/Title/tt0413300/" }, { "cover": "http://ia.media-imdb.com/images/M/MV5BMjE1ODcyODYxMl5BMl5BanBnXkFtZTcwNjA1NDE3MQ@@._V1._SX54_CR0,0,54,74_.jpg", "Title": "SpIDer-Man 2", "year": "(2004", "director": "Sam Raimi", "rating": "7.5", "details": "http://www.imdb.com/Title/tt0316654/" } ]}}
我得到了所有的数据…但我无法显示我从“封面”标签获得的图像网址…从这里开始做什么最简单的方法..? http://ia.media-imdb.com/images/M/MV5BMjMyOTM4MDMxNV5BMl5BanBnXkFtZTcwNjIyNzExOA@@._V1._SX54_CR0,0,54,74_.jpg
我已经从http Json数据在ListvIEw中实现了AndroID Lazy Loading图像和文本,现在我想让列表项可以点击,我无法弄清楚如何为这个案例做到这一点
public class MainActivity extends Activity {ListVIEw mListVIEw;@OverrIDepublic voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main); final TextVIEw txts=(TextVIEw) findVIEwByID(R.ID.textVIEw1); String URL1=getIntent().getExtras().getString("URL"); // URL to the JsON data String strUrl = URL1; // Creating a new non-ui thread task to download Json data DownloadTask downloadTask = new DownloadTask(); // Starting the download process downloadTask.execute(strUrl); // Getting a reference to ListVIEw of activity_main mListVIEw = (ListVIEw) findVIEwByID(R.ID.lv_countrIEs); } /** A method to download Json data from url */ private String downloadUrl(String strUrl) throws IOException{ String data = ""; inputStream iStream = null; try{ URL url = new URL(strUrl); // Creating an http connection to communicate with url httpURLConnection urlConnection = (httpURLConnection) url.openConnection(); // Connecting to url urlConnection.connect(); // Reading data from url iStream = urlConnection.getinputStream(); BufferedReader br = new BufferedReader(new inputStreamReader(iStream)); StringBuffer sb = new StringBuffer(); String line = ""; while( ( line = br.readline()) != null){ sb.append(line); } data = sb.toString(); br.close(); }catch(Exception e){ Log.d("Exception while downloading url", e.toString()); }finally{ iStream.close(); } return data; } /** AsyncTask to download Json data */ private class DownloadTask extends AsyncTask<String, Integer, String>{ String data = null; @OverrIDe protected String doInBackground(String... url) { try{ data = downloadUrl(url[0]); }catch(Exception e){ Log.d("Background Task",e.toString()); } return data; } @OverrIDe protected voID onPostExecute(String result) { // The parsing of the xml data is done in a non-ui thread ListVIEwLoaderTask ListVIEwLoaderTask = new ListVIEwLoaderTask(); // Start parsing xml data ListVIEwLoaderTask.execute(result); } } /** AsyncTask to parse Json data and load ListVIEw */ private class ListVIEwLoaderTask extends AsyncTask<String, VoID, SimpleAdapter>{ JsONObject jObject; // Doing the parsing of xml data in a non-ui thread @OverrIDe protected SimpleAdapter doInBackground(String... strjson) { try{ jObject = new JsONObject(strjson[0]); CountryJsONParser countryJsonParser = new CountryJsONParser(); countryJsonParser.parse(jObject); }catch(Exception e){ Log.d("JsON Exception1",e.toString()); } // Instantiating Json parser class CountryJsONParser countryJsonParser = new CountryJsONParser(); // A List object to store the parsed countrIEs List List<HashMap<String, Object>> countrIEs = null; try{ // Getting the parsed data as a List construct countrIEs = countryJsonParser.parse(jObject); }catch(Exception e){ Log.d("Exception",e.toString()); } // Keys used in Hashmap String[] from = { "country","flag","details"}; // IDs of vIEws in ListvIEw_layout int[] to = { R.ID.tv_country,R.ID.iv_flag,R.ID.tv_country_details}; // Instantiating an adapter to store each items // R.layout.ListvIEw_layout defines the layout of each item SimpleAdapter adapter = new SimpleAdapter(getBaseContext(), countrIEs, return adapter; } /** Invoked by the AndroID on "doInBackground" is executed */ @OverrIDe protected voID onPostExecute(SimpleAdapter adapter) { // Setting adapter for the ListvIEw mListVIEw.setAdapter(adapter); for(int i=0;i<adapter.getCount();i++){ HashMap<String, Object> hm = (HashMap<String, Object>) adapter.getItem(i); String imgurl = (String) hm.get("flag_path"); ImageLoaderTask imageLoaderTask = new ImageLoaderTask(); HashMap<String, Object> hmDownload = new HashMap<String, Object>(); hm.put("flag_path",imgurl); hm.put("position", i); // Starting ImageLoaderTask to download and populate image in the ListvIEw imageLoaderTask.execute(hm); } } } /** AsyncTask to download and load an image in ListVIEw */ private class ImageLoaderTask extends AsyncTask<HashMap<String, Object>, VoID, HashMap<St @OverrIDe protected HashMap<String, Object> doInBackground(HashMap<String, Object>... hm) { inputStream iStream=null; String imgurl = (String) hm[0].get("flag_path"); int position = (Integer) hm[0].get("position"); URL url; try { url = new URL(imgurl); // Creating an http connection to communicate with url httpURLConnection urlConnection = (httpURLConnection) url.openConnection(); // Connecting to url urlConnection.connect(); // Reading data from url iStream = urlConnection.getinputStream(); // Getting Caching directory file cacheDirectory = getBaseContext().getCacheDir(); // Temporary file to store the downloaded image file tmpfile = new file(cacheDirectory.getPath() + "/wpta_"+position+".png"); // The fileOutputStream to the temporary file fileOutputStream fOutStream = new fileOutputStream(tmpfile); // Creating a bitmap from the downloaded inputstream Bitmap b = BitmapFactory.decodeStream(iStream); // Writing the bitmap to the temporary file as png file b.compress(Bitmap.CompressFormat.PNG,100, fOutStream); // Flush the fileOutputStream fOutStream.flush(); //Close the fileOutputStream fOutStream.close(); // Create a hashmap object to store image path and its position in the ListvIEw HashMap<String, Object> hmBitmap = new HashMap<String, Object>(); // Storing the path to the temporary image file hmBitmap.put("flag",tmpfile.getPath()); // Storing the position of the image in the ListvIEw hmBitmap.put("position",position); // Returning the HashMap object containing the image path and position return hmBitmap; }catch (Exception e) { e.printstacktrace(); } return null; } @OverrIDe protected voID onPostExecute(HashMap<String, Object> result) { // Getting the path to the downloaded image String path = (String) result.get("flag"); // Getting the position of the downloaded image int position = (Integer) result.get("position"); // Getting adapter of the ListvIEw SimpleAdapter adapter = (SimpleAdapter ) mListVIEw.getAdapter(); // Getting the hashmap object at the specifIEd position of the ListvIEw HashMap<String, Object> hm = (HashMap<String, Object>) adapter.getItem(position); // Overwriting the existing path in the adapter hm.put("flag",path); // Noticing ListvIEw about the dataset changes adapter.notifyDataSetChanged(); }} @OverrIDe public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_main, menu); return true; } }
如何做呢 ?
解决方法:
首先请看一些教程,如:
http://www.androidhive.info/2012/01/android-json-parsing-tutorial/
http://www.vogella.com/articles/AndroidJSON/article.html
JsONObject response = new JsONObject(respString);ArrayList<Book> bookCollection = new ArrayList<Book>();// if(response.has("results")){ if(response.has("result")){ JsONArray resultArray = response.getJsONArray("result"); for(int iCount=0; iCount<resultArray.length; iCount++){ JsONObject item = resultArray.getJsONObject(iCount); Book book = new Book(); if(item.has("Title")){ book.Title =item.getString("Title"); } bookCollection.add(book); } } }
填充的pojo
public Book{public String cover;public String Title;}
总结 以上是内存溢出为你收集整理的如何在android中解析复杂的JSON文件全部内容,希望文章能够帮你解决如何在android中解析复杂的JSON文件所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)