问题描述
服务器rest发送json内容如下:{"to":"140678489983418860","type":"groupchat","msg":"xiaoxi","ext":{"msg":"xiaoxi","msgid":"90BEE52A87FB043B73BA334BFED14247","yonghuid":"55d6576abbfd4b4aaa6113e1e0124b4d","shuohuarenfrom":{"yonghuid":"55d6576abbfd4b4aaa6113e1e0124b4d","yonghunicheng":"包长青的小号","yonghubiaozhu":"0","yonghutouxiang":"templates/wdcs/image/icon/0007.jpg","zhanghaoleixing":"6"},"shuohuarentoperson":null,"topersonnicheng":null,"shuohuarentogrop":"140678489983418860","shuohuashijian":"2016-03-23 09:30:06","xiaoxileibie":0,"shuohuaneirong":"ddd","yishenhe":1,"yishanchu":0,"yihuifu":0,"yizhiding":0}}android 端接收消息代码如下: EMMessageListener messageListener = new EMMessageListener() { @Override public void onMessageReceived(List<EMMessage> list) { String jsonResponse = JSON.toJSONString(list); Log.d("---------新消息", jsonResponse); for (EMMessage message : list) { // 群组消息 if (message.getChatType() == EMMessage.ChatType.GroupChat) { //收到消息 try { Log.d("---------", "说话内容" + message.getStringAttribute("shuohuaneirong")); Log.d("---------", "用户ID" + message.getStringAttribute("yonghuid")); Log.d("---------", "说话人来自" + message.getJSONObjectAttribute("shuohuarenfrom")); // 获取不到// Log.d("---------", "说话人来自" + message.getStringAttribute("shuohuarenfrom")); // 获取不到// message.get... // 获取不到 } catch (HyphenateException e) { e.printStackTrace(); } } } }问题:android端可以接受到说话内容及用户ID,但"说话人来自(json中的shuohuarenfrom)"数据无论使用message.get什么方法都是获取不到的,如何解决?
解决方案
可以使用message.getJSONArrayAttribute(java.lang.String attribute)获取 JSONArray 类型扩展属性message.JSONObject getJSONObjectAttribute(java.lang.String attribute)获取 JSONObject 类型扩展属性