Click here to Skip to main content
14,838,412 members
Please Sign up or sign in to vote.
2.33/5 (3 votes)
See more:
[{"linkName":"Microsoft","linkPublish":"True","linkLogo":"","linkURL":" ","linkID":"10","linkTimeStamp":"3/23/2012 3:38:55 AM","linkPreviousID":"-1"},{"linkName":"RBA","linkPublish":"True","linkLogo":"","linkURL":"","linkID":"11","linkTimeStamp":"3/23/2012 3:38:55 AM","linkPreviousID":"-1"},{"linkName":"ViaWest","linkPublish":"True","linkLogo":"","linkURL":"","linkID":"12","linkTimeStamp":"3/23/2012 3:38:55 AM","linkPreviousID":"9"},{"linkName":"RBA","linkPublish":"True","linkLogo":"","linkURL":"","linkID":"14","linkTimeStamp":"3/23/2012 3:38:55 AM","linkPreviousID":"11"},{"linkName":"Microsoft","linkPublish":"True","linkLogo":"","linkURL":" ","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....


public class Main extends ListActivity {
	/** Called when the activity is first created. */
    public void onCreate(Bundle savedInstanceState) {
        ArrayList<hashmap><string,>> mylist = new ArrayList<hashmap><string,>>();
        JSONObject json = JSONfunctions.getJSONfromURL("");
        	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"));
        }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[] {, });
        final ListView lv = getListView();
        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(); 





public class JSONfunctions {

	public static JSONObject getJSONfromURL(String url){
		InputStream is = null;
		String result = "";
		JSONObject jArray = null;
		//http post
	            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
	            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");
	    }catch(Exception e){
	            Log.e("log_tag", "Error converting result "+e.toString());
            jArray = new JSONObject(result);            
	    }catch(JSONException e){
	            Log.e("log_tag", "Error parsing data "+e.toString());
	    return jArray;
Updated 24-Aug-12 10:51am
xenotron 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!!
xenotron 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