在实际开发过程中,免不了和其他公司进行联调,调用第三方接口,这个时候我们就需要根据对方返回的数据进行解析,获得我们想要的字段
第一种
//这种是data里面有个list的格式
{"data": {"username": "system","roles": [ {"groupId": null,"urt": "6cd3d213-1574-49a8-8fc7-81816575ca5c","manageUnit": null,"roleName": "系统管理员","manaunitname": "黄陂区卫生局","manaunitid": "420116","roleId": "chis.system"}],"realname": "系统管理员","mobile": null},"code": "200","msg": "success"
}
通过对象来接收
//创建对象
@Data
public class StationDoctorVo {@ApiModelProperty(value = "用户名")private String username;@ApiModelProperty(value = "角色名称")private String realname;@ApiModelProperty(value = "手机号码")private String mobile;@ApiModelProperty(value = "角色列表")private List<Map<String,Object>> roles;
}
import com.google.gson.Gson;String result = "{"data": {"username": "system","roles": [ {"groupId": null,"urt": "6cd3d213-1574-49a8-8fc7-81816575ca5c","manageUnit": null,"roleName": "系统管理员","manaunitname": "黄陂区卫生局","manaunitid": "420116","roleId": "chis.system"}],"realname": "系统管理员","mobile": null},"code": "200","msg": "success"
}"
Gson gson = new Gson();
JsonObject jsonObject = gson.fromJson(result, JsonObject.class);
StationDoctorVo stationDoctorVo = gson.fromJson(jsonObject.get("data"),StationDoctorVo.class);
List<Map<String, Object>> roles = stationDoctorVo.getRoles();
if (roles != null && roles.size() > 0) {//这里获取第一个是因为他返回可能是多个,一个人多个角色Map<String, Object> map1 = roles.get(0);System.out.println(map1.get("manaunitid").toString());System.out.println(map1.get("manaunitname").toString());System.out.println(map1.get("urt").toString());
}
第二种
//这种是最简单的格式了
{"data": {"doctorId": "grcs","isOk": 0,"doctorName": "光荣测试"},"code": "200","msg": "success"
}
第一种接收方式:直接get
Gson gson = new Gson();
JsonObject jsonObject4 = gson.fromJson(result2, JsonObject.class);
//获取外层code的值
String code = jsonObject4.get("code").getAsString();
//获取data的值
JsonObject jsonObject1 = jsonObject4.getAsJsonObject("data");
//获取内层doctorName
String msg = jsonObject1.get("doctorName").getAsString();
第二种:使用对象接收
//创建对象
@Data
public class PatientExistVo {@ApiModelProperty(value = "是否新建标志 0: 不能新建 1: 可以新建 2:注销")private String isOk;@ApiModelProperty(value = "责任医生id (有档案情况下返回)")private String doctorId;@ApiModelProperty(value = "责任医生名字(有档案情况下返回)")private String doctorName;
}
Gson gson = new Gson();
JsonObject jsonObject = gson.fromJson(result, JsonObject.class);
//先通过外层code判断是否成功返回
if ("200".equals(jsonObject.get("code").getAsString())) {//成功返回就用对象接收数据PatientExistVo patientExistVo = gson.fromJson(jsonObject.get("data"),PatientExistVo.class);
}
附带请求json
//他们需要的数据格式
{"uid":"xkcs","token": "16555222a3e14d389682a8618ae636e3","op": "create","data": {"geda": {"cardNo": "","personName": "天下第一","photo": ""},"person": {"cardNo": "","personName": "天下第一","sexCode": "1","birthday": "1941-02-15"}}
}
我们需要创建3个对象,然后一个对象里嵌套其他2个对象
@Data
public class PatientPushDoctorVo {@ApiModelProperty(value = "身份证号码")private String idCard;@ApiModelProperty(value = "医生工号")private String uid;@ApiModelProperty(value = "公卫系统token")private String token;@ApiModelProperty(value = "修改还是新增,create")private String op;@ApiModelProperty(value = "医生所属机构id")private String manaunitid;@ApiModelProperty(value = "医生所属机构名称")private String manaunitname;private Geda geda;private Person person;
}
然后在通过String param12 = gson.toJson(vo);转成String
String param = gson.toJson(vo);
//得到String,我们就可以通过这个param去请求了
同理,如果他们需要的只是普通的,我们就直接创建一个对象,或者使用map,然后转成String就ok了
Map<String, Object> map = new HashMap<>();
map.put("uid", vo.getUid());
map.put("token", vo.getToken());
map.put("idCard", vo.getIdCard());
String param = gson.toJson(map);