如何在列表视图中解析此类型的json数据 [英] how to parse this type of json data in listview

查看:68
本文介绍了如何在列表视图中解析此类型的json数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

[{"linkName":"Microsoft","linkPublish":"True","linkLogo":"http://www.ishirsoft.com/AHM/AHM/images/cfbab4b1-92fa-4401-ae4c-f921259ea5b9.jpg","linkURL":"http://www.microsoft.com ","linkID":"10","linkTimeStamp":"3/23/2012 3:38:55 AM","linkPreviousID":"-1"},{"linkName":"RBA","linkPublish":"True","linkLogo":"http://www.ishirsoft.com/AHM/AHM/images/919635d7-4bfa-4ee3-b9c9-3091153f12ea.jpg","linkURL":"http://www.rbaconsulting.com","linkID":"11","linkTimeStamp":"3/23/2012 3:38:55 AM","linkPreviousID":"-1"},{"linkName":"ViaWest","linkPublish":"True","linkLogo":"http://www.ishirsoft.com/AHM/AHM/images/7e266ef5-3f71-46ab-8a0a-86ca9516178a.png","linkURL":"http://www.viawest.com","linkID":"12","linkTimeStamp":"3/23/2012 3:38:55 AM","linkPreviousID":"9"},{"linkName":"RBA","linkPublish":"True","linkLogo":"http://www.ishirsoft.com/AHM/AHM/images/60c809d6-7937-41b3-934b-fb4d249a7f20.png","linkURL":"http://www.rbaconsulting.com","linkID":"14","linkTimeStamp":"3/23/2012 3:38:55 AM","linkPreviousID":"11"},{"linkName":"Microsoft","linkPublish":"True","linkLogo":"http://www.ishirsoft.com/AHM/AHM/images/7e12c518-7336-4829-88da-526722960e21.png","linkURL":"http://www.microsoft.com ","linkID":"13","linkTimeStamp":"3/23/2012 3:38:55 AM","linkPreviousID":"10"}]



从上面的json数据中,我必须在列表视图中绑定链接名称和linkurl....任何人都可以帮忙,问题是我是新手,并且我已经看过很多教程,但是这些教程确实很复杂,大多数教程都使用相同的教程例子....地震就这么多!但是这些json数据包含{"Earthquakekes :: [{" eqid:" c0001xgp," magnitude:8.8,....我在方括号前加了花括号,但您所看到的我的json数据却有所不同!我已经成功运行了这些示例,但是当我使用链接时,应用程序崩溃了,所以请分享您的知识以帮助我!谢谢

这是我正在使用的代码....


导入...



From above json data i have to bind link name and linkurl in listview....can anybody please help,the problem is i am a newbie and and i have gone through many tutorials but those were really complicated,most of them use the same example....earthquakes n all that! but those json data contains { "earthquakes":[{"eqid":"c0001xgp","magnitude":8.8,.... i,e curly braces before the square brackets but mine json data is different as u can see! i have sucessfully run these examples but when i use my link the app crashes, so please share your knowledge to help me ! thanks

this is the code i am using....


import...

public class Main extends ListActivity {
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.listplaceholder);
        
        ArrayList<hashmap><string,>> mylist = new ArrayList<hashmap><string,>>();
      Log.i("development",mylist.toString());
       
        JSONObject json = JSONfunctions.getJSONfromURL("http://api.geonames.org/earthquakesJSON?north=44.1&south=-9.9&east=-22.4&west=55.2&username=demo");
                
        try{
        	
        	JSONArray  earthquakes = json.getJSONArray("earthquakes");
        	
	        for(int i=0;i<earthquakes.length();i++){>
				HashMap<string,> map = new HashMap<string,>();	
				JSONObject e = earthquakes.getJSONObject(i);
				
				map.put("id",  String.valueOf(i));
	        	map.put("name", "Earthquake name:" + e.getString("eqid"));
	        	map.put("magnitude", "Magnitude: " +  e.getString("magnitude"));
	        	mylist.add(map);			
			}		
        }catch(JSONException e)        {
        	 Log.e("log_tag", "Error parsing data "+e.toString());
        }
        
        ListAdapter adapter = new SimpleAdapter(this, mylist , R.layout.main, 
                        new String[] { "name", "magnitude" }, 
                        new int[] { R.id.item_title, R.id.item_subtitle });
        
        setListAdapter(adapter);
        
        final ListView lv = getListView();
        lv.setTextFilterEnabled(true);	
        lv.setOnItemClickListener(new OnItemClickListener() {
        	public void onItemClick(AdapterView        		@SuppressWarnings("unchecked")
				HashMap<string,> o = (HashMap<string,>) lv.getItemAtPosition(position);	        		
        		Toast.makeText(Main.this, "ID ''" + o.get("id") + "'' was clicked.", Toast.LENGTH_SHORT).show(); 

			}
		});
    }

}





导入......



and

import....

public class JSONfunctions {

	public static JSONObject getJSONfromURL(String url){
		InputStream is = null;
		String result = "";
		JSONObject jArray = null;
		
		//http post
	    try{
	            HttpClient httpclient = new DefaultHttpClient();
	            HttpPost httppost = new HttpPost(url);
	            HttpResponse response = httpclient.execute(httppost);
	            HttpEntity entity = response.getEntity();
	            is = entity.getContent();

	    }catch(Exception e){
	            Log.e("log_tag", "Error in http connection "+e.toString());
	    }
	    
	  //convert response to string
	    try{
	            BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
	            StringBuilder sb = new StringBuilder();
	            String line = null;
	            while ((line = reader.readLine()) != null) {
	                    sb.append(line + "\n");
	            }
	            is.close();
	            result=sb.toString();
	    }catch(Exception e){
	            Log.e("log_tag", "Error converting result "+e.toString());
	    }
	    
	    try{
	    	
            jArray = new JSONObject(result);            
	    }catch(JSONException e){
	            Log.e("log_tag", "Error parsing data "+e.toString());
	    }
    
	    return jArray;
	}
}

推荐答案

http://www.androidhive.info/2012/01/android-json-parsing-tutorial/ [ http://adblogcat.com/parse-json-data -from-a-web-server-and-display-on-listview/ [ http://prabhakaranchandrasekaran.blogspot.com/2012/08/android-tutorial-how-to-parse-read-json.html [
http://www.androidhive.info/2012/01/android-json-parsing-tutorial/[^]
http://adblogcat.com/parse-json-data-from-a-web-server-and-display-on-listview/[^]
http://prabhakaranchandrasekaran.blogspot.com/2012/08/android-tutorial-how-to-parse-read-json.html[^]


这篇关于如何在列表视图中解析此类型的json数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆