java websocket将list转json传给微信小程序的问题

先看问题在这里插入图片描述
这是后端返回的json 但是data.id是取不到的
后端使用gson.toString将list转为json

这时需要将小程序的返回值var objData = JSON.parse(res.data);

json.parse() 方法将数据转换为 JavaScript 对象( 将字符串转成json对象。 )

下面是小程序js代码


  onLoad: function (options) {
    console.log(app.globalData.openid);
  },
  m:function(){
    //建立连接
    var that=this;
    wx.connectSocket({
      url: "ws://127.0.0.1:9090/xcxmvc/so",
    })

    //连接成功
    wx.onSocketOpen(function () {
      console.log("c" + app.globalData.openid);
      wx.sendSocketMessage({
        data: app.globalData.openid,
      })
    })
    wx.onSocketMessage(function (res) {
      var objData = JSON.parse(res.data);
      console.log(res);
      that.setData({ nr: objData});
    })


    //连接失败
    wx.onSocketError(function () {
      console.log('websocket连接失败!');
    })
  
  },

下面是后台代码

package so;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;

import com.google.gson.Gson;

import net.sf.json.groovy.GJson;
import bean.dao;
import bean.nrbean;

@ServerEndpoint("/so")
public class soc {


@OnOpen
public void open(Session c){
	System.out.print("ok"+c.getId()+"...");
	//c=sc;
	
}
@OnMessage
public void onmessage(final String data,final Session s){
	System.out.println(data+s.getId());
	 
	final String shou="shoudao";
	
	TimerTask task = new TimerTask() { //创建一个新的timer task 
		public void run() { //定时器任务执行的操作
			Gson gson = new Gson();
			 dao dd= new dao();
			 HashMap<String,List> aa=new HashMap<String,List>();
			 
			// List aa=null;
			// nrbean n=new nrbean();
			// List chakan=new ArrayList();
			 List<nrbean> li=new ArrayList<nrbean>();// po=null;
		        li=dd.chakan(data);
		       
		       // List nr=null;
		     //nr=dd.getAlljiezhexie(o);
		     //aa.put("hfnr",dd.getAllhf(o));
		        
		     //   me m=	g.fromJson(mingzi,me.class);//(mingzi);
		     aa.put("nr",li);
		     String jsonObject = gson.toJson(li);
		        System.out.print("~~~~~~~~~~~~~~~~~"+jsonObject);
			try {
				s.getBasicRemote().sendText(jsonObject);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				System.out.print("fa song cuo le");
			}
			System.out.println("现在是:" +data);
			
		}};
	
	
	Timer timer = new Timer();//创建一个定时器
	long delay = 0;
	long PeriodTime = 1 * 1000;
	timer.scheduleAtFixedRate(task, delay, PeriodTime);
	//重复执行特定任务,第一个参数为要执行的任务,第二个为执行任务之前延迟的时间,第三个为时间间隔
	//单位都是毫秒
	
		
		
}


}

socket

package websocket;
import java.util.Set;

import javax.websocket.Endpoint;
import javax.websocket.server.ServerApplicationConfig;
import javax.websocket.server.ServerEndpointConfig;
public class socket implements ServerApplicationConfig{

	@Override
	public Set<Class<?>> getAnnotatedEndpointClasses(Set<Class<?>> scan) {
		// TODO Auto-generated method stub
		System.out.println("config........."+scan.size());
		return scan;
	}

	@Override
	public Set<ServerEndpointConfig> getEndpointConfigs(
			Set<Class<? extends Endpoint>> arg0) {
		// TODO Auto-generated method stub
		return null;
	}

}

猜你喜欢

转载自blog.csdn.net/weixin_40938312/article/details/104702790