微信后端代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

191 lines
6.4 KiB

package com.ynxbd.common.service;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;
import java.util.ArrayList;
import java.util.List;
@Slf4j
public class TestResult {
@Data
@NoArgsConstructor
@ToString
public static class TestDoctor {
private String docCode;
private String docName;
}
//
// public static void main(String[] args) {
// String json = "{\n" +
// " \"Code\": \"200\",\n" +
// " \"Msg\": \"SUCCESS\",\n" +
// " \"Data\": [\n" +
// " {\n" +
// " \"docCode\": \"0002\",\n" +
// " \"docName\": \"李卫\"\n" +
// " },\n" +
// " {\n" +
// " \"docCode\": \"0003\",\n" +
// " \"docName\": \"杨淑\"\n" +
// " },\n" +
// " {\n" +
// " \"docCode\": \"0005\",\n" +
// " \"docName\": \"马晓琳\"\n" +
// " },\n" +
// " {\n" +
// " \"docCode\": \"0010\",\n" +
// " \"docName\": \"张永云\"\n" +
// " },\n" +
// " {\n" +
// " \"docCode\": \"0011\",\n" +
// " \"docName\": \"蔡连勇\"\n" +
// " },\n" +
// " {\n" +
// " \"docCode\": \"0012\",\n" +
// " \"docName\": \"罗琳辉\"\n" +
// " },\n" +
// " {\n" +
// " \"docCode\": \"0013\",\n" +
// " \"docName\": \"屈继波\"\n" +
// " },\n" +
// " {\n" +
// " \"docCode\": \"0015\",\n" +
// " \"docName\": \"李瀛\"\n" +
// " },\n" +
// " {\n" +
// " \"docCode\": \"0016\",\n" +
// " \"docName\": \"杨阳\"\n" +
// " },\n" +
// " {\n" +
// " \"docCode\": \"0017\",\n" +
// " \"docName\": \"江为民\"\n" +
// " }\n" +
// " ],\n" +
// " \"Note\": {\n" +
// " \"funcName\": \"DeanSearch_wechatDataPush\",\n" +
// " \"funcNote\": \"微信推送数据\",\n" +
// " \"connTime\": 30,\n" +
// " \"inParams\": {},\n" +
// " \"isListOutParams\": true,\n" +
// " \"outParams\": {\n" +
// " \"docCode\": \"医生code\",\n" +
// " \"docName\": \"医生name\"\n" +
// " }\n" +
// " }\n" +
// "}";
//
// String respData = getRespData(json);
// System.out.println(respData);
// List<TestDoctor> testDoctors = jsonToList(respData, TestDoctor.class);
// System.out.println(testDoctors);
//
// List<String> doctCodeList = jsonToList(respData, "docCode");
// System.out.println(doctCodeList);
// }
/**
* 获取请求中的数据
*/
public static String getRespData(String respData) {
if (respData == null) {
return null;
}
JSONObject jsonResp = JSON.parseObject(respData);
String code = jsonResp.getString("Code");
String message = jsonResp.getString("Msg");
if ("200".equals(code) && "SUCCESS".equals(message)) {
String data = jsonResp.getString("Data");
return "".equals(data) ? null : data;
}
log.info("[请求失败]code={}, message={}", code, message);
return null;
}
public static <T> List<T> jsonToList(String data, Class<T> clazz) {
if (data == null) {
return new ArrayList<>();
}
String jsonStr = JSON.toJSONString(data);
if ("".equals(jsonStr)) {
return new ArrayList<>();
}
try {
return JSON.parseArray(data, clazz);
} catch (Exception e) {
log.error(e.getMessage());
return new ArrayList<>();
}
}
/**
* 获取对象数组中某个名称的值,加入集合
*
* @param data json数据
* @param name 属性名称
* @return 属性List
*/
public static List<String> jsonToList(String data, String name) {
List<String> resultList = new ArrayList<>();
if (data == null) {
return resultList;
}
String jsonStr = JSON.toJSONString(data);
if ("".equals(jsonStr)) {
return resultList;
}
try {
JSONArray jsonArr = JSON.parseArray(data);
JSONObject itemObj;
String val;
for (int i = 0; i < jsonArr.size(); i++) {
itemObj = jsonArr.getJSONObject(i);
if (itemObj == null) {
continue;
}
val = itemObj.getString(name);
if (val != null && !"".equals(val)) {
resultList.add(val);
}
}
return resultList;
} catch (Exception e) {
log.error(e.getMessage());
}
return resultList;
}
/**
* json转实体类
*
* @param data 数据
* @param clazz 类型
* @return 对象
*/
public static <T> T jsonToBean(String data, Class<T> clazz) {
if (data == null) {
return null;
}
String jsonStr = JSON.toJSONString(data);
if ("".equals(jsonStr)) {
return null;
}
try {
return JSON.parseObject(data, clazz);
} catch (Exception e) {
log.error(e.getMessage());
return null;
}
}
}