数据的转换,我的代码(垃圾)和我的师傅的代码!
来源:互联网 发布:河南大学软件学院图片 编辑:程序博客网 时间:2024/06/12 01:22
我要做的事情是这样的!我要和一个C++对接!C++给传一个数据,我来进行处理,然后添加到Es中去,说白了也就是我写了一个接口!下面我们来看看我的代码!
首先C++给我的数据是这样的!
String jsonStr="
{
"bandWidth": {
"actualRecvBwApplication": 0,
"actualRecvBwAudio": 0,
"actualRecvBwMax:": 1534,
"actualRecvBwVideo:": 1534,
"actualSendBwApplication:": 0,
"actualSendBwAudio": 0,
"actualSendBwMax": 1905,
"actualSendBwVideo:": 1545,
"availRecvBwMax": 100000,
"availSendBwMax:": 1905,
"truelyNetData": true
},
"localdatetime": "2017-07-10T15:19:41",
"localtimestamp": 1499671181257,
"orgkey": "c71badcc543e4c968275025f1896ec5f",
"shaperInfo": {
"delayAppPriorityNormal": 0,
"delayAppPriorityRetransmit": 0,
"delayVideoPriorutyRetransmit": 0,
"delayVideoPriorytyNormal": 71,
"numDroppedAppPriorityNormal": 0,
"numDroppedVideoPriorytyNormal": 0,
"numFramesAppPriorityNormal": 0,
"numFramesVideoPriorytyNormal": 0,
"numPacketsAppPriorityNormal": 0,
"numPacketsAppPriorityRetransmit": 0,
"numPacketsVideoPriorutyRetransmit": 0,
"numPacketsVideoPriorytyNormal": 7,
"truelyRateShaperData": true
},
"vedioInfo": {
"decodedFrameRate": 29,
"displayedFrameRate": 29,
"height": 720,
"receivedFrameRate": 29,
"truelyParticipantData": true,
"width": 1280
}
}
";"bandWidth": {
"actualRecvBwApplication": 0,
"actualRecvBwAudio": 0,
"actualRecvBwMax:": 1534,
"actualRecvBwVideo:": 1534,
"actualSendBwApplication:": 0,
"actualSendBwAudio": 0,
"actualSendBwMax": 1905,
"actualSendBwVideo:": 1545,
"availRecvBwMax": 100000,
"availSendBwMax:": 1905,
"truelyNetData": true
},
"localdatetime": "2017-07-10T15:19:41",
"localtimestamp": 1499671181257,
"orgkey": "c71badcc543e4c968275025f1896ec5f",
"shaperInfo": {
"delayAppPriorityNormal": 0,
"delayAppPriorityRetransmit": 0,
"delayVideoPriorutyRetransmit": 0,
"delayVideoPriorytyNormal": 71,
"numDroppedAppPriorityNormal": 0,
"numDroppedVideoPriorytyNormal": 0,
"numFramesAppPriorityNormal": 0,
"numFramesVideoPriorytyNormal": 0,
"numPacketsAppPriorityNormal": 0,
"numPacketsAppPriorityRetransmit": 0,
"numPacketsVideoPriorutyRetransmit": 0,
"numPacketsVideoPriorytyNormal": 7,
"truelyRateShaperData": true
},
"vedioInfo": {
"decodedFrameRate": 29,
"displayedFrameRate": 29,
"height": 720,
"receivedFrameRate": 29,
"truelyParticipantData": true,
"width": 1280
}
}
下面是我的处理代码
@RequestMapping(value = "/postdata" , method = RequestMethod.POST)public ResponseResult posts( HttpServletRequest request, String jsonStr) throws UnknownHostException { outputRequest(request); Map map1 = new LinkedHashMap<>(); if("".equals(jsonStr)){ return new ResponseResult(PaasExceptionDict.DataFormatError); } //System.out.println("++++++++++++++++++++++++++++++++++++++"+jsonStr); //解析json对象 Map map = new HashMap(); Gson gson = new Gson(); JSONObject jsonObject = JSONObject.parseObject(jsonStr); for (Map.Entry<String, Object> entry : jsonObject.entrySet()) { //System.out.println("#############"+entry.getKey()+"==== "+entry.getValue()); if("bandWidth".equals(entry.getKey())){ JSONObject jsonObject1 = JSONObject.parseObject(entry.getValue().toString()); for (Map.Entry<String, Object> entry1 : jsonObject1.entrySet()) { map.put(entry1.getKey(),entry1.getValue()); /*if("flase".equals(entry.getValue())){ return new ResponseResult(PaasExceptionDict.DataFormatError); }*/ } } if("vedioInfo".equals(entry.getKey())){ JSONObject jsonObject1 = JSONObject.parseObject(entry.getValue().toString()); for (Map.Entry<String, Object> entry1 : jsonObject1.entrySet()) { map.put(entry1.getKey(),entry1.getValue()); } } if("shaperInfo".equals(entry.getKey())){ JSONObject jsonObject1 = JSONObject.parseObject(entry.getValue().toString()); for (Map.Entry<String, Object> entry1 : jsonObject1.entrySet()) { map.put(entry1.getKey(),entry1.getValue()); } } if("orgkey".equals(entry.getKey())){ map.put(entry.getKey(),entry.getValue()); } if("localtimestamp".equals(entry.getKey())){ map.put(entry.getKey(),entry.getValue()); } if("localDateTime".equals(entry.getKey())){ map.put(entry.getKey(),entry.getValue()); } } long timestamp=new Date().getTime(); //System.out.println("时间戳是:"+timestamp); map.put("servertimestamp",timestamp); //时间戳转换成国际时间 SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"); String format1 = format.format(timestamp); map.put("serverDateTime",format1); String json = gson.toJson(map); //System.out.println("===================================== "+json); //添加数据 InsertDataEs(json); map1.put("json", json); return new ResponseResult(true,map1);}下面是我师傅的代码!
@RequestMapping(value = "/putdata" , method = RequestMethod.POST)public ResponseResult posts( HttpServletRequest request, @RequestBody Map<String,Object> data ) throws UnknownHostException { outputRequest(request); if(data==null){ System.out.println("data is null"); return new ResponseResult(false,false); } Gson gson=new Gson(); long timestamp=new Date().getTime(); //System.out.println("时间戳是:"+timestamp); data.put("servertimestamp",timestamp); //时间戳转换成国际时间 SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"); String format1 = format.format(timestamp); data.put("serverdatetime",format1); String json = gson.toJson(data); InsertDataEs(json); return new ResponseResult(true,true);}
下面就是公共的代码!
//添加数据public ResponseResult InsertDataEs(String json) throws UnknownHostException { //System.out.println(json); TransportClient client; //连接client System.out.println("create TransportClient..."); client = new PreBuiltTransportClient(Settings.EMPTY) .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(""), 9300)) .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(""), 9300)) .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(""), 9300)); IndexRequestBuilder indexRequestBuilder = client.prepareIndex("vdyoo", "desktop", "ID"+ IdentifierKeyHelper.getDateTimeLetterIdKey(5)).setSource(json); IndexResponse response = indexRequestBuilder.get(); //关闭client if (client != null) { client.close(); System.out.println("TransportClient close..."); } return new ResponseResult(PaasExceptionDict.InsertError);}我写了三四天!而且他别慢!
我的师傅写了三分钟!而且特别快!
我的内心是奔溃的!
希望和我一样菜的哥们赶紧学习吧!
阅读全文
0 0
- 数据的转换,我的代码(垃圾)和我的师傅的代码!
- 我的一个垃圾代码。。。
- 我的师傅
- 我的师傅叫追逐
- 我的代码我负责
- 我不喜欢我的代码
- 我又不是做蛋糕的师傅
- 王师傅教我的单调栈
- 我的代码
- 我的代码箱
- 保护我的代码
- 我的代码格式
- 我用的代码
- 我的数据库代码
- 我的分页代码
- 我的一些代码
- 我的代码人生
- 我的代码心得
- CSDN日报20170710——《小公司程序员怎么进大公司》
- 【技能库】--mysql 索引失效 force index也失效-- 原因解决方案(256)
- 解决Maven项目pom.xml文件报xxx\target\classes\META-INF\MANIFEST.MF (系统找不到指定的路径。)问题
- 公钥和密钥解释
- Android Service
- 数据的转换,我的代码(垃圾)和我的师傅的代码!
- markdowng
- Common Lisp环境篇(slime+sbcl)
- PHP之FTP、SFTP上传下载
- WebService基础学习(一)—基础知识
- Android 使用富文本显示web上的内容
- JVM垃圾收集器
- Python中list,tuple,dict,set基本定义和基本用法
- freemarker自动生成代码模板