【问题标题】:Json Parsing Android in Listview在 Listview 中解析 A​​ndroid 的 Json
【发布时间】:2014-11-06 10:18:04
【问题描述】:

我正在使用包含消息和时间令牌的 pubnub 获取我的历史列表的应用程序。原始Json字符串如下:

[[{"message":"hdjcjcjjckckckckvkckckck","timetoken":14151866297757284},{"message":"ufjfjfjcjfjchfjdhwjroritkgjcj","timetoken":14151869212145693},{"message":"qMobile","timetoken":14152601234812083}],14151866297757284,14152601234812083]

使用以下方法后,我在 JSONObject c 中得到了以下 Json 字符串。

{"message":"hdjcjcjjckckckckvkckckck","timetoken":14151866297757284}


pubnub.history(channel, true, 100, new Callback() {
                            @Override
                            public void successCallback(String channel,
                                                        Object message) {
                                notifyUser("HISTORY : " + message);

                              Log.i("Received msg : ", message.toString());  //<==== receiving Messages here


                              try {

                                JSONArray jsonObj = new JSONArray(message.toString()); 
                                JSONArray jArray = new JSONArray(jsonObj.get(0).toString());
                                for (int i = 0; i < jArray.length(); i++) {

                                    JSONObject c = jArray.getJSONObject(i);

                                    String messageString=c.getString("message");
                                    String timeString=c.getString("timetoken");
                                    String abc = timeString;

                                }

                            } 

现在我只想像这样在 ListView 中显示我的每个 Json 对象

消息:dsfdsvsfvdfvfdvdvgd
时间令牌:14132423414141
消息:dsfwfwefwedcsfsw
时间令牌:21431353153252
消息:dthfjtyhnfgvb
时间令牌:68624526246

提前致谢。

【问题讨论】:

  • 然后实现ListAdapter并展示出来。
  • @pratt 感谢您的回复。你能为此提供一些代码sn-p吗?因为我没有为此获得 ListAdapter 技术。
  • 为什么你不是 Gson api.. 试试看很简单
  • 检查这个链接androidhive.info/2012/01/android-json-parsing-tutorial这可能对你有帮助..
  • @Chowdary102 我点击了您的链接,但它不允许我下载代码。如果您已经下载了副本,请在此处告诉我

标签: android json parsing android-listview pubnub


【解决方案1】:

声明一个 ArrayList 的 hashmap 如下:

ArrayList<HashMap<String,String>> data= new ArrayList<HashMap<String,String>>();
HashMap<String,String> jsonData;
private EfficientAdapter adapter;

//In your onCreate() method, just initialize your adapter.
adapter = new EfficientAdapter(this);

现在,在这个 for 循环中:

for (int i = 0; i < jArray.length(); i++) {
    jsonData=new HashMap<String,String>();
    JSONObject c = jArray.getJSONObject(i);

    String messageString=c.getString("message");
    String timeString=c.getString("timetoken");
    String abc = timeString;

    jsonData.put("message",messageString);
    jsonData.put("timeStamp",timeString);

    data.add(jsonData);
}

YOURLISTVIEW.setAdapter(adapter);

//列出适配器代码:

private class EfficientAdapter extends BaseAdapter {

        private LayoutInflater mInflater;
        private Context context;

        public EfficientAdapter(Context context) {
            mInflater = LayoutInflater.from(context);
            this.context = context;
        }

        @Override
        public int getCount() {
            return data.size();
        }

        @Override
        public Object getItem(int arg0) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {

            final ViewHolder holder;

            if (convertView == null) {
                convertView = mInflater.inflate(R.layout.YOUR LAYOUT FILE TO SHOW MESSAGE AND TIME, null);
                //Just create one xml in layout folder and give that layout file name like R.layout.list_item.
                holder = new ViewHolder();
                holder.txtMessage = (TextView) convertView.findViewById(R.id.txtMessage);
                holder.txtTimestamp = (TextView) convertView.findViewById(R.id.txtTimestamp);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            holder.txtMessage.setText(data.get(position).get("message"));
            holder.txtTimestamp.setText(data.get(position).get("timeStamp"));

            convertView.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    //your list click event here
                }
            });

            return convertView;
        }

        class ViewHolder {
            TextView txtMessage;
            TextView txtTimestamp;
        }

    }

【讨论】:

  • 感谢@pratt 的详细回复。我正在使用上面的代码 sn-p。我只需要了解以下代码: for (int i = 0; i (); JSONObject c = jArray.getJSONObject(i);字符串 messageString=c.getString("message"); String timeString=c.getString("timetoken"); jsonData.put("消息",messageString); jsonData.put("timeStamp",timeString); data.add(jsonData);} YOURLISTVIEW.setAdapter(适配器); //我们在更新适配器值的地方,从上面的代码中我们只是做了它,但没有在其中传递值。
  • 你会得到它,我们正在将你所有的 json 数据添加到名为“data”的数组中,这样你也可以在 Adapter 中获取这些数据,用你现有的 Activity 创建 Adapter 类,而不是 2nd 类。跨度>
【解决方案2】:

更好的选择是使用 GSON,它会在未来通过 json 解析节省您的时间。

  1. 下载gson并将其添加到您的项目http://search.maven.org/#artifactdetails%7Ccom.google.code.gson%7Cgson%7C2.3%7Cjar
  2. 粘贴您的 json - http://www.jsonschema2pojo.org/,并制作一个 java 类表示。
  3. 使用您的 json 字符串(或 Reader)、您创建的类(在第 2 点中)并获取一个具有 json 具有的所有值的对象 -

    Gson gson = new Gson(); yourObject = gson.fromJson(yourJsonString, yourObject.class);

    1. 然后在你的适配器中使用这个对象。例如 - message = yourObject.get(position).getMessage();

【讨论】:

    猜你喜欢
    • 2014-08-18
    • 2017-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多