Click here to Skip to main content
16,016,306 members
Please Sign up or sign in to vote.
2.33/5 (3 votes)
See more:
[{"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"}]


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....

C#
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;
	}
}
Posted
Updated 24-Aug-12 10:51am
v4
Comments
pasztorpisti 24-Aug-12 17:18pm    
Why do you copy your data here with some piece of totally unrelated source code??? If you are waiting for someone to modify the source code you copy pasted from the internet then you will be disappointed. JSON is the simplest of the data description languages next to xml. Even a newbie must be able to use it especially after reading through a tutorial. Noone believes that you tried hard...
azhar eqbal 28-Aug-12 6:32am    
sir! with due respect...if you can not help me,that is fine,but i don't think that anybody has right to comment on my efforts,coz it is only me who knows how much i have tried,and one more thing sir,newbies like me dont post questions and codes just to have our work done by some intelligent and experienced persons like you,but we really need help.The thing you find easy some one can find that most complicated and vise versa.Anyways thanks to you for wasting your valuable time in my silly question!!
pasztorpisti 28-Aug-12 8:59am    
OK, maybe I misunderstood you but then what is the question? If your question is in the title: "how to parse this type of json data in listview" then it's already answered below.

1 solution

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900