Json を使用して ROR でデータを解析したい、すべてのデータを配列内の名前として解析したい
ホテルコントローラー.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 %>
}
私はこのコードを書きます、このコードが正しいか間違っているか、配列のように名前(データ)を解析します。
これは私の解析用 URL です。
http://peaceful-cliffs-6253.herokuapp.com/hotels.json、そのURLで、配列内のすべての名前を解析したいのですが、コードでそれを行うにはどうすればよいですか
私の解析出力は次のように表示されます [ { "name": "karthi" }, { "name": "shreshtt" }, { "name": "jitu" }, { "name": null }, { "name" : null }, { "name": null } ] しかし、このモデルのように表示したいのですが、どうすればこのようにできますか
{
"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"
}
},
それは私がやりたい上記のモデルのようなものです、
ホテルでは、すべての名前を表示する必要があります
jsonを使用してAndroidで名前の配列を表示する方法、
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 hotel = null; にあると思います。名前を表示せずにプレーンな画面のみを表示するandroidの出力、