如何为我的代码编写json文件?解析数据还没有在android中显示?

时间:2013-05-10 11:04:57

标签: android ruby-on-rails json parsing

我想使用Json通过ROR解析我的数据,我想将所有数据解析为数组中的名称

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,在那个网址中我想解析一个数组中的所有名字,我怎么能在我的代码中做到这一点

我的解析输出显示如下 [   {     “名字”:“karthi”   },   {     “名字”:“shreshtt”   },   {     “名字”:“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的输出只显示普通屏幕而不显示名称,

1 个答案:

答案 0 :(得分: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