delphi 企业微信消息机器人_企业微信—群聊机器人
在企業(yè)微信群聊機器人接口對接天氣API使用過程中,遇到?過一個問題,就是對于嵌套json數(shù)據(jù)如何進行嵌套的|
一:"msgtype": "text",
curl 'http://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=633a31f6-7f9c-4bc4-97a0-0ec1eefa5898' \
-H 'Content-Type: application/json' \
-d '
{
"msgtype": "text",
"text": {
"content": "hello world"
}
}'
這是第一種情況:
//請求天氣API
? ? ? String weather = HttpRequest.sendGet("https://www.tianqiapi.com/api/", "version=v1");
? ? ? JSONObject text = new JSONObject();
? ? ? //獲得天氣的json格式的字符串,將字符串轉json對象
? ? ? JSONObject weatherJson = JSONObject.parseObject(weather);
//將剛獲得的json對象中的data鍵值
? ? ? JSONArray ja = weatherJson.getJSONArray("data");
//獲得data數(shù)據(jù)組中第一組數(shù)據(jù)(一共有5天的天氣數(shù)據(jù),只取了第一天的數(shù)據(jù)存入weatherJson1 )
? ? ? JSONObject weatherJson1 = JSONObject.parseObject(ja.get(0).toString());
? ? ? System.out.println(weatherJson1.get("air_tips"));
? ? ? text.put("content", weatherJson.get("update_time").toString()+ "數(shù)據(jù),東宇鴻翔氣象發(fā)布:\r\n濰坊: ?" + weatherJson1.get("wea")+"\r\n"+ "最高溫度:" + weatherJson1.get("tem1") + "\r\n"+ "最低溫度:" + weatherJson1.get("tem2") + "\r\n" + "\r\n空氣指數(shù):"+ ""+weatherJson1.get("air")+"" + "\r\n空氣質(zhì)量:"+""+weatherJson1.get("air_level")+"\r\n" + weatherJson1.get("air_tips"));
//定義一新jsonobject
? ? ? JSONObject msg = new JSONObject();
//定義格式
? ? ? msg.put("msgtype", "markdown");
放入json值
? ? ? msg.put("markdown", text);
對接的企業(yè)微信機器人URL
? ? ? String url = "企業(yè)微信機器人webhook地址";
? ? ? String result = InterfaceUtils.sendPost(url,msg.toJSONString(),"");
? ? ? System.out.println(result);
二、"msgtype": "news",類型
{
"msgtype": "news",
"news": {
"articles" : [
{
"title" : "中秋節(jié)禮品領取",
"description" : "今年中秋節(jié)公司有豪禮相送",
"url" : "URL",
"picurl" : "http://res.mail.qq.com/node/ww/wwopenmng/images/independent/doc/test_pic_msg1.png"
}
]
}
}
String weather = HttpRequest.sendGet("https://www.tianqiapi.com/api/", "version=v1"); ? ? ? ?
? ? ? String result;
? ? ? //將字符串轉json
? ? ? JSONObject weatherJson = JSONObject.parseObject(weather);
? ? ? Object jaArticle = weatherJson.get("data");
? ? ? System.out.println(jaArticle);
? ? ? JSONArray jsonSonData = weatherJson.getJSONArray("data");
? ? ? System.out.println("L38:" + jsonSonData.get(0));
// ? ? ?JSONObject weatherJson1 = JSONObject.parseObject(jsonSonData.get(0).toString());
? ? ? String data = weatherJson.get("data").toString().substring(1);
? ? ? data ?= data.substring(0,data.length()-1);
? ? ? //第二部分中上面這段數(shù)據(jù)可能沒用,下面對象直接賦死值,是為了測試在連接企業(yè)微信機器人json值的使用格式,請見諒。
? ? ? Article article = new Article();//定義對象
? ? ? article.setTitle("abc");//給對象賦值
? ? ? article.setDescription("abc");
? ? ? article.setUrl("https://mp.weixin.qq.com/s/Lca666663Vhib_Fbn8sXDk1qCig?company_from66=a42b8d663ced2a11e88d5b52540005f4367767865");
? ? ? article.setPicurl("http://res.mail.qq.com/node/ww/wwopenmng/images/independent/doc/test_pic_msg1.png");
? ? ? List listArticles = new ArrayList();//定義泛型
? ? ? listArticles.add(article);//List數(shù)組賦值,可以add多個,親測有效
? ? ? JSONObject msg = new JSONObject();//大json
? ? ? JSONObject msg1 = new JSONObject();//小json/**
? ? ? ?*如果將put第二個參數(shù)toString(),不能將其轉為JSON字符串。
? ? ? ?* msg1.put("articles", listArticles.toString());toString()會在json對應位置多加一地引號,導致下面報錯
? ? ? ?* 則
? ? ? ?* "article size out of limit, hint: [1566700084_6_35eebe8628c6d0b3066d76116fff3a17]"}
? ? ? ?*/
? ? ? msg1.put("articles", listArticles);
? ? ? msg.put("msgtype", "news");//格式
? ? ? msg.put("news", msg1);
// ? ? ?msg.put("news", jsArticle);
? ? ? System.out.println( "*********77:" + msg.toJSONString());
? ? ? System.out.println("*********************");
? ? ? String url = "企業(yè)微信機器人webhook地址";
? ? ? result = InterfaceUtils.sendPost(url,msg.toJSONString(),"");
? ? ? System.out.println(result);
制作不易,點個贊吧。
總結
以上是生活随笔為你收集整理的delphi 企业微信消息机器人_企业微信—群聊机器人的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 原生html表格属性
- 下一篇: docker卸载命令_Docker入门篇