dataByCityName 字符串格式 : {"msg":"success","code":0,"data":[]}
json数据
{
"msg": "success",
"code": 0,
"data": [
{
"effective": "2022/01/11 04:20",
"latitude": 22.5497,
"description": "深圳市气象台于01月11日04时20分发布深圳市强季风蓝色预警信号,请注意防御。",
"id": "44030041600000_20220111042251",
"title": "广东省深圳市发布强季风蓝色预警",
"type": "11B80_BLUE",
"headline": "深圳市气象台发布强季风蓝色预警",
"longitude": 114.0862
}
]
}
可以看出,我们需要的数据中,data其实是一个list对象。
controller:
@AutoLog(value = "天气预警-中国气象局")
@ApiOperation(value = "天气预警-中国气象局", notes = "天气预警-中国气象局")
@GetMapping("/getWarnInfo")
public Result<?> getWarnInfo() {
String dataByCityName = weatherService.getWeatherWarn();
JSONObject jsonObject = JSONObject.parseObject(dataByCityName);
JSONArray jsonArray = jsonObject.getJSONArray("data");
try {
List<WarnWeatherHistory> warnWeatherHistories = jsonArray.toJavaList(WarnWeatherHistory.class);
if (!warnWeatherHistories.isEmpty()){
WarnWeatherHistory warnWeatherHistory = warnWeatherHistories.get(0);
WarnWeatherHistory warnWeatherHistory1 = weatherWarnHistoryService.getById(warnWeatherHistory.getId());
if (warnWeatherHistory1 == null){
// 如果数据不存在
weatherWarnHistoryService.save(warnWeatherHistory);
}
}
} catch (Exception e) {
e.printStackTrace();
}
return Result.OK(jsonObject);
}
实体类:
/**
*
* @author Ryan
* @since 2022-01-11
*/
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@TableName("warn_weather_history")
@ApiModel(value="WeatherWarnHistory对象", description="")
public class WarnWeatherHistory implements Serializable {
private static final long serialVersionUID=1L;
@ApiModelProperty("主键id")
@TableId(value = "id", type = IdType.ASSIGN_ID)
private String id;
@ApiModelProperty("标题")
private String title;
@ApiModelProperty("类型")
private String type;
@ApiModelProperty("标题")
private String headline;
@ApiModelProperty("经度")
private BigDecimal longitude;
@ApiModelProperty("纬度")
private BigDecimal latitude;
@ApiModelProperty(value = "影响时间",notes = "格式:yyyy-MM-dd")
private String effective;
@ApiModelProperty("描述")
private String description;
@TableField(exist = false)
@JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd")
@DateTimeFormat(pattern = "yyyy-MM-dd")
@ApiModelProperty(value = "开始时间",notes = "格式:yyyy-MM-dd")
private String startTimes;
@TableField(exist = false)
@JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd")
@DateTimeFormat(pattern = "yyyy-MM-dd")
@ApiModelProperty(value = "结束时间",notes = "格式:yyyy-MM-dd")
private String endTimes;
}
这里,实体类字段要对应上,否侧jsonobject有可能解析失败.
数据成功写入数据库~
版权声明:本文为m0_59259076原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。