我正在制作一个tcp服务器并决定使用json进行打包(我称之为)。
客户端是c#,服务器是java
客户发送:
byte[] b1 = Encoding.UTF8.GetBytes({\"action\":\"login\",\"age\":20}");
服务器:
Charset charset = Charset.forName( "UTF-8" );
ByteBuffer inBuff = evt.getInputBuffer(); // Input buffer
System.out.println(charset.decode(inBuff)); // out: {"action":"login","age":20}
JSONObject obj = new JSONObject(charset.decode(inBuff));
System.out.println(obj.toString()); // out: {}, THE PROBLEM
答案 0 :(得分:0)
您需要将CharBuffer转换为临时变量中的String,然后从该变量中创建对象:
String temp = charset.decode(inBuff).toString();
JSONObject obj = new JSONObject(temp);
String action = obj.getString("action");
System.out.println(action); //