我想通過使用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的輸出只顯示普通屏幕而不顯示名稱,
不知道的明白,你不需要index.json.erb,這是自動的Rails – Galen 2013-05-10 11:08:52
心不是響應已經有數組管理?你只想發送一個只包含名字的數組給瀏覽器? – 2013-05-10 11:08:58
雅,我想發送tha數組回到瀏覽器使用的網址,我想給的數組名稱,在酒店裏面它必須顯示,但它不顯示這樣,它只會顯示名稱數組@AlokSwain – 2013-05-10 11:10:45