使用Java将JSON数组和对象转换为表视图

时间:2015-02-26 16:49:12

标签: java android json

JSON看起来像:

   [{"pmid":"2","name":"MANAGEMENT","result":"1","properties":[{"prop_id":"32","prop_name":" Tower","address":"281 Lakeshore","city":"Euclid","state":"OH","zip":"44142","lat":"54.5","long":"-81.5034"}]},{"pmid":"1","name":"ONE","result":"18","properties":[{"prop_id":"3","prop_name":"Chase","address":"146 Goon Blvd.","city":"City","state":"OH","zip":"12345","lat":"46.35","long":"-83.1138"},{"prop_id":"6","prop_name":"Club Apartments","address":"4600 Barrington Club","city":"Columbus","state":"OH","zip":"43520","lat":"40.436","long":"-83.048"}]}]

使用Android Studio,尝试使用:

进行检索
private static String url = "http://appurl.com/apis/pagement_list.php";
// JSON Node names
private static final String TAG_CONTACTS = "properties";
private static final String TAG_NAME = "prop_id";
private static final String TAG_EMAIL = "prop_id";
private static final String TAG_PHONE = "prop_id";

命名节点后,我正在使用以下内容检索JSON:

   @Override
    protected Void doInBackground(Void... arg0) {
        // Creating service handler class instance
        ServiceHandler sh = new ServiceHandler();



        // Making a request to url and getting response
        String jsonStr = sh.makeServiceCall(url, ServiceHandler.GET);

        Log.d("Response: ", "> " + jsonStr);


        if (jsonStr != null) {
            // We create out JSONObject from the data
            JSONObject jObj = null;
            try {
                JSONArray mJsonArray = new JSONArray(jsonStr);
                JSONObject mJsonObject = mJsonArray.getJSONObject(0);

                String pmid = mJsonObject.getString("pmid");
                String name = mJsonObject.getString("name");
                String result = mJsonObject.getString("result");


                JSONArray mJsonArrayProperty = mJsonObject.getJSONArray("properties");
                for (int i = 0; i < mJsonArrayProperty.length(); i++) {
                    JSONObject mJsonObjectProperty = mJsonArrayProperty.getJSONObject(i);

                    String prop_id = mJsonObjectProperty.getString("prop_id");
                    String prop_name = mJsonObjectProperty.getString("prop_name");
                    String address = mJsonObjectProperty.getString("address");
                    String city = mJsonObjectProperty.getString("city");
                    String state = mJsonObjectProperty.getString("state");
                    String zip = mJsonObjectProperty.getString("zip");
                    String lat = mJsonObjectProperty.getString("lat");
                    String lon = mJsonObjectProperty.getString("long");

                    // tmp hashmap for single contact
                    HashMap<String, String> contact = new HashMap<String, String>();

                    // adding each child node to HashMap key => value
                    contact.put(TAG_EMAIL, prop_name);
                    contact.put(TAG_NAME, name);
                    contact.put(TAG_PHONE, address);




                    // adding contact to contact list
                    contactList.add(contact);
                }

            } catch (JSONException e) {
                e.printStackTrace();
            }

        }
            else {
                Log.e("ServiceHandler", "Couldn't get any data from the url");
            }

            return null;
        }
    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);


        // Dismiss the progress dialog
        if (pDialog.isShowing())
            pDialog.dismiss();
        /**
         * Updating parsed JSON data into ListView
         * */
        ListAdapter adapter = new SimpleAdapter(
                MainActivity.this, contactList,
                R.layout.list_item, new String[] { TAG_NAME, TAG_EMAIL, TAG_PHONE}, new int[] { R.id.name,
                R.id.email, R.id.mobile});

        setListAdapter(adapter);


    }

}

我能够成功地在TableView中检索第一个带有Lakeshore地址的Array,Tower。但是,我不知道如何获得JSON的其他部分,例如另一个名称。

如果名称可以显示在包含prop_name的数组之前,那将会很棒。并且名称只需要显示一次,如果下面有多个prop_name。

1 个答案:

答案 0 :(得分:0)

我不知道我是否正确理解了你的问题,但对我来说,代码看起来只是在<第二行开始之后只提取外部数组的第一个元素的子数组strong>尝试{阻止:

JSONObject mJsonObject = mJsonArray.getJSONObject(0);

你应该做的事情如下:

try {
  JSONArray mJsonArray = new JSONArray(jsonStr);
  int length = mJsonArray.length();
  for (int i = 0; i < length; i++) {
    JSONObject mJsonObject = mJsonArray.getJSONObject(i);
    ...
    String name = mJsonObject.getString("name");
    JSONArray mJsonArrayProperty = mJsonObject.getJSONArray("properties");
    int innerLength = mJsonArrayProperty.length();
    for (int k = 0; k < innerLength; k++) {
      JSONObject mJsonObjectProperty = mJsonArrayProperty.getJSONObject(k);
      String prop_name = mJsonObjectProperty.getString("prop_name");
      String address = mJsonObjectProperty.getString("address");
      ...
      // hashmap for single contact
      HashMap<String, String> contact = new HashMap<String, String>();
      // adding each child node to HashMap key => value
      contact.put(TAG_EMAIL, prop_name);
      contact.put(TAG_NAME, name);
      contact.put(TAG_PHONE, address);
      // adding contact to contact list
      contactList.add(contact);
    }
  }
} catch (JSONException e) {
  e.printStackTrace();
}
...

请注意,内部for循环的计数器 k 与外部循环 i 的计数器不同。 hashmap不是临时的,它被传递给集合的add方法,因此幸存下来。

相关问题