ruby on rails - How to write the json file for my code?the parsing data its not displaying in android also? -
i want parse data ror using json,i want parse data name in array
hotelscontroller.erb
respond_to :json, :xml def index @hotels = hotel.all respond_to |format| format.html # show.html.erb format.json { render json: @hotels.to_json(:only => [ :name ]) } end end
index.json.erb
{ hotel: <% @hotels.each |hotel| %> { 'name': "<%= hotel.name %>" } <% end %> }
i write code,is code correct or wrong,it parse name(data) in array.
this parsing url,
http://peaceful-cliffs-6253.herokuapp.com/hotels.json, in url want parse names in array, how can in code
my parsing output showing [ { "name": "karthi" }, { "name": "shreshtt" }, { "name": "jitu" }, { "name": null }, { "name": null }, { "name": null } ] want show model,how can this
{ "contacts": [ { "id": "c200", "name": "ravi tamada", "email": "ravi@gmail.com", "address": "xx-xx-xxxx,x - street, x - country", "gender": "male", "phone": { "mobile": "+91 0000000000", "home": "00 000000", "office": "00 000000" } }, { "id": "c201", "name": "johnny depp", "email": "johnny_depp@gmail.com", "address": "xx-xx-xxxx,x - street, x - country", "gender": "male", "phone": { "mobile": "+91 0000000000", "home": "00 000000", "office": "00 000000" } }, { "id": "c202", "name": "leonardo dicaprio", "email": "leonardo_dicaprio@gmail.com", "address": "xx-xx-xxxx,x - street, x - country", "gender": "male", "phone": { "mobile": "+91 0000000000", "home": "00 000000", "office": "00 000000" } },
its above model want do,
with hotel names have display
how display array of names in android using json,
public class androidjsonparsingactivity extends listactivity { // url make request private static string url = "http://peaceful-cliffs-6253.herokuapp.com/hotels.json"; // json node names //private static final string tag_hotel = "hotel"; //private static final string tag_id = "id"; private static final string tag_name = "name"; //private static final string tag_email = "email"; //private static final string tag_address = "address"; //private static final string tag_gender = "gender"; //private static final string tag_phone = "phone"; //private static final string tag_phone_mobile = "mobile"; //private static final string tag_phone_home = "home"; //private static final string tag_phone_office = "office"; // contacts jsonarray jsonarray hotel; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); strictmode.threadpolicy policy = new strictmode.threadpolicy.builder().permitall().build(); strictmode.setthreadpolicy(policy); // hashmap listview arraylist<hashmap<string, string>> contactlist = new arraylist<hashmap<string, string>>(); // creating json parser instance jsonparser jparser = new jsonparser(); // getting json string url jsonobject json = jparser.getjsonfromurl(url); try { // getting array of contacts hotel = json.getjsonarray(tag_name); // looping through contacts for(int = 0; < hotel.length(); i++){ jsonobject c = hotel.getjsonobject(i); // storing each json item in variable //string id = c.getstring(tag_id); string name = c.getstring(tag_name); log.e("name testing", name); //string email = c.getstring(tag_email); //string address = c.getstring(tag_address); //string gender = c.getstring(tag_gender); // phone number agin json object //jsonobject phone = c.getjsonobject(tag_phone); //string mobile = phone.getstring(tag_phone_mobile); //string home = phone.getstring(tag_phone_home); //string office = phone.getstring(tag_phone_office); // creating new hashmap hashmap<string, string> map = new hashmap<string, string>(); // adding each child node hashmap key => value //map.put(tag_id, id); map.put(tag_name, name); //map.put(tag_email, email); //map.put(tag_phone_mobile, mobile); // adding hashlist arraylist contactlist.add(map); } } catch (jsonexception e) { e.printstacktrace(); } /** * updating parsed json data listview * */ listadapter adapter = new simpleadapter(this, contactlist, r.layout.list_item, new string[] { tag_name, }, new int[] { r.id.name,}); setlistadapter(adapter); // selecting single listview item listview lv = getlistview(); // launching new screen on selecting single listitem lv.setonitemclicklistener(new onitemclicklistener() { @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { // getting values selected listitem string name = ((textview) view.findviewbyid(r.id.name)).gettext().tostring(); //string cost = ((textview) view.findviewbyid(r.id.email)).gettext().tostring(); //string description = ((textview) view.findviewbyid(r.id.mobile)).gettext().tostring(); // starting new intent intent in = new intent(getapplicationcontext(), singlemenuitemactivity.class); in.putextra(tag_name, name); //in.putextra(tag_email, cost); //in.putextra(tag_phone_mobile, description); startactivity(in); } }); } }
this client side code also,its not fetches array of names,i think problem might in jsonarray hotel = null; output of android displaying plain screen without displaying names,
you don't need index.json.erb. type way in hotelscontroller.erb:
def index @hotels = hotel.all respond_to |format| format.html # index.html.erb hash = {:hotels => @hotels} format.json { render :json => hash } end
Comments
Post a Comment