zhouzeyu 1 年之前
父節點
當前提交
7e0c9efe6a
共有 1 個文件被更改,包括 28 次插入20 次删除
  1. 28 20
      src/main/java/com/tuoren/forward/netty/common/JsonExtractor.java

+ 28 - 20
src/main/java/com/tuoren/forward/netty/common/JsonExtractor.java

@@ -19,28 +19,36 @@ public class JsonExtractor {
             //创建新的JSON对象
             JSONObject newJson = new JSONObject();
 
-
             //从原始JSON中提取需要的字段
-            newJson.put("code", orginalObj.getIntValue("code"));
-            newJson.put("version", orginalObj.getIntValue("version"));
-            newJson.put("platform", orginalObj.getIntValue("platform"));
-            newJson.put("networkType", orginalObj.getIntValue("networkType"));
-            newJson.put("networkProtocol", orginalObj.getIntValue("networkProtocol"));
-            newJson.put("productId", orginalObj.getString("productId"));
-            newJson.put("userId", orginalObj.getString("userId"));
-            newJson.put("deviceId", orginalObj.getString("deviceId"));
-            newJson.put("address", orginalObj.getString("address"));
-            newJson.put("port", orginalObj.getIntValue("port"));
-            newJson.put("deviceSecret", orginalObj.getString("deviceSecret"));
-            //老版本兼容
-            newJson.put("deviceName", orginalObj.getString("deviceName"));
-            newJson.put("productKey", orginalObj.getString("productKey"));
-            Object wifi = orginalObj.get("wifi");
-            if (wifi instanceof String) {
-                // 如果 wifi 是字符串,尝试将其解析为 JSON 数组
-                wifi = JSON.parseArray((String) wifi);
+            //如果解析的json字符串含有version字段,区分新老版本
+            if (orginalObj.getIntValue("version") == 1) {
+                //新版本
+                newJson.put("code", orginalObj.getIntValue("code"));
+                newJson.put("version", orginalObj.getIntValue("version"));
+                newJson.put("platform", orginalObj.getIntValue("platform"));
+                newJson.put("networkType", orginalObj.getIntValue("networkType"));
+                newJson.put("networkProtocol", orginalObj.getIntValue("networkProtocol"));
+                newJson.put("productId", orginalObj.getString("productId"));
+                newJson.put("userId", orginalObj.getString("userId"));
+                newJson.put("deviceId", orginalObj.getString("deviceId"));
+                newJson.put("remoteAddress", orginalObj.getString("remoteAddress"));
+                newJson.put("remotePort", orginalObj.getIntValue("remotePort"));
+                newJson.put("deviceSecret", orginalObj.getString("deviceSecret"));
+                Object wifi = orginalObj.get("wifi");
+                if (wifi instanceof String) {
+                    // 如果 wifi 是字符串,尝试将其解析为 JSON 数组
+                    wifi = JSON.parseArray((String) wifi);
+                }
+                newJson.put("wifi", wifi);
+
+            } else {
+                //老版本
+                newJson.put("address", orginalObj.getString("address"));
+                newJson.put("port", orginalObj.getIntValue("port"));
+                newJson.put("deviceName", orginalObj.getString("deviceName"));
+                newJson.put("deviceSecret", orginalObj.getString("deviceSecret"));
+                newJson.put("productKey", orginalObj.getString("productKey"));
             }
-            newJson.put("wifi", wifi);
 
             //将新的JSON对象转换成字符串
             return newJson.toJSONString();