java - 使用 Java 将 JSON 数组和对象转换为 TableView

标签 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 中检索第一个数组 Tower,其中包含 Lakeshore 地址。但是,我不知道如何获取 JSON 的其他部分,例如其他名称。

如果名称可以显示在包含 prop_name 的数组之前,那就太好了。并且该名称只需要显示一次,以防其下有多个prop_name。

最佳答案

我不知道我是否正确理解你的问题,但对我来说,代码看起来只是在 try { block 开始后的第二行中提取外部数组第一个元素的子数组:

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 方法,因此仍然存在。

关于java - 使用 Java 将 JSON 数组和对象转换为 TableView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28748094/

相关文章:

java - 无阻塞地读取 Socket 的可用内容

java - 如何用 rest 返回 boolean 值?

android - 如何从适用于 Android 的 Google Places API 获取地点详细信息?

javascript - jQuery 奇怪的 JSON 行为

javascript - 在angularjs中使用来自rest服务的JSON对象

java - Netbeans 8.0.1 的 MySQL 安装程序

java - 检索 JTextField 文本值

android - WebView 杀死了整个 Activity --- 我该如何调试它?

android - 如何检测用户是否在我的应用程序上使用广告拦截器?

javascript - Vue.js 消费 json