2013-05-10 36 views
0

我想通过使用JSON ROR解析我的数据,我想分析阵列中的如何为我的代码编写json文件?它的解析数据在android中也不显示?

hotelscontroller.erb

respond_to :json, :xml 
    def index 
    @hotels = Hotel.all 

    respond_to do |format| 
     format.html # show.html.erb 
     format.json { render json: @hotels.to_json(:only => [ :name ]) } 
    end 
    end 

index.json.erb

{ 
hotel: <% @hotels.each do |hotel| %> 
    { 'name': "<%= hotel.name %>" } 
    <% end %> 
} 
所有的数据名字

我写这段代码,是这段代码是正确还是错误,它会解析数组中的名字(数据)。

这是我的解析地址,

http://peaceful-cliffs-6253.herokuapp.com/hotels.json,该网址我想分析阵列中的所有的名字,我怎么能做到这一点在我的代码

我解析输出显示这样 [ { “名称”: “卡” },{ “名称”: “shreshtt” },{ “名称”: “激凸” },{ “南E“:空 }, { ”名“:空 }, { ”名“:空 } ] 但我想说明这样的模式,我怎么可以这样做

{ 
    "contacts": [ 
    { 
     "id": "c200", 
     "name": "Ravi Tamada", 
     "email": "[email protected]", 
     "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": "[email protected]", 
     "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": "[email protected]", 
     "address": "xx-xx-xxxx,x - street, x - country", 
     "gender": "male", 
     "phone": { 
     "mobile": "+91 0000000000", 
     "home": "00 000000", 
     "office": "00 000000" 
     } 
    }, 

它像上面的模式,我想做的事情,

与酒店的所有名称必须显示

如何在Android中使用JSON显示名称的阵列,

public class AndroidJSONParsingActivity extends ListActivity { 

    // url to 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 for ListView 
     ArrayList<HashMap<String, String>> contactList = new ArrayList<HashMap<String, String>>(); 

     // Creating JSON Parser instance 
     JSONParser jParser = new JSONParser(); 

     // getting JSON string from URL 
     JSONObject json = jParser.getJSONFromUrl(url); 

     try { 
      // Getting Array of Contacts 
      hotel = json.getJSONArray(TAG_NAME); 

      // looping through All Contacts 
      for(int i = 0; i < 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 is 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 to 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 to ArrayList 
       contactList.add(map); 
      } 
     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 


     /** 
     * Updating parsed JSON data into 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 from 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); 

      } 
     }); 



    } 

} 

这是我的客户端代码也,它不是取名字的数组,我想问题可能是JSONArray酒店= NULL; android的输出只显示普通屏幕而不显示名称,

+0

不知道的明白,你不需要index.json.erb,这是自动的Rails – Galen 2013-05-10 11:08:52

+0

心不是响应已经有数组管理?你只想发送一个只包含名字的数组给浏览器? – 2013-05-10 11:08:58

+0

雅,我想发送tha数组回到浏览器使用的网址,我想给的数组名称,在酒店里面它必须显示,但它不显示这样,它只会显示名称数组@AlokSwain – 2013-05-10 11:10:45

回答

0

你不需要index.json.erb。只要输入这种方式在hotelscontroller.erb:

def index 
    @hotels = Hotel.all 

    respond_to do |format| 
     format.html # index.html.erb 
     hash = {:hotels => @hotels} 
     format.json { render :json => hash } 
    end 
相关问题