在企业微信群聊机器人接口对接天气API使用过程中,遇到 过一个问题,就是对于嵌套json数据如何进行嵌套的
一:"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数据组中第一组数据(一共有5天的天气数据,只取了第一天的数据存入weatherJson1 )
JSONObject weatherJson1 = JSONObject.parseObject(ja.get(0).toString());
System.out.println(weatherJson1.get("air_tips"));
text.put("content", weatherJson.get("update_time").toString()+ "数据,东宇鸿翔气象发布:\r\n潍坊: " + weatherJson1.get("wea")+"\r\n"+ "最高温度:" + weatherJson1.get("tem1") + "\r\n"+ "最低温度:" + weatherJson1.get("tem2") + "\r\n" + "\r\n空气指数:"+ "<font color=\\\"warning\\\">"+weatherJson1.get("air")+"</font>" + "\r\n空气质量:"+"<font color=\\\"warning\\\">"+weatherJson1.get("air_level")+"</font>\r\n" + weatherJson1.get("air_tips"));
//定义一新jsonobject
JSONObject msg = new JSONObject();
//定义格式
msg.put("msgtype", "markdown");
放入json值
msg.put("markdown", text);
对接的企业微信机器人URL
String url = "企业微信机器人webhook地址";
String result = InterfaceUtils.sendPost(url,msg.toJSONString(),"");
System.out.println(result);
二、 "msgtype": "news",类型
"msgtype": "news",类型
-
{
-
"msgtype": "news",
-
"news": {
-
"articles" : [
-
{
-
"title" : "中秋节礼品领取",
-
"description" : "今年中秋节公司有豪礼相送",
-
"url" : "URL",
-
"picurl" : "http://res.mail.qq.com/node/ww/wwopenmng/images/independent/doc/test_pic_msg1.png"
-
}
-
]
-
}
-
}
企业微信群聊机器人一:"msgtype": "text",二、"msgtype": "news",类型
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);
//第二部分中上面这段数据可能没用,下面对象直接赋死值,是为了测试在连接企业微信机器人json值的使用格式,请见谅,如果有需要,可以联系我,将上面代码改为天气数据QQ:58925561
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<Article> listArticles = new ArrayList<Article>();//定义泛型
listArticles.add(article);//List数组赋值,可以add多个,亲测有效
JSONObject msg = new JSONObject();//大json
JSONObject msg1 = new JSONObject();//小json
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 = "企业微信机器人webhook地址";
result = InterfaceUtils.sendPost(url,msg.toJSONString(),"");
System.out.println(result);