完成json反序列化。

This commit is contained in:
Kane Wang 2022-03-17 16:40:09 +08:00
parent 5825b7a929
commit 479eb32f7e
6 changed files with 67 additions and 55 deletions

View File

@ -58,6 +58,7 @@
<artifactId>jackson-annotations</artifactId> <artifactId>jackson-annotations</artifactId>
<version>2.9.8</version> <version>2.9.8</version>
</dependency> </dependency>
</dependencies> </dependencies>
<properties> <properties>
<maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.source>1.8</maven.compiler.source>

View File

@ -1,12 +1,31 @@
import com.cpic.xim.disaster_warning.QWeatherDisasterWarning;
import com.cpic.xim.disaster_warning.WeatherDisasterWarningGrabber; import com.cpic.xim.disaster_warning.WeatherDisasterWarningGrabber;
import java.io.IOException;
public class AppMain public class AppMain
{ {
public static void main( String[] args ) public static void main( String[] args )
{ {
String cityCode = "101091103"; String cityCode = "101230201";
String json;
QWeatherDisasterWarning warning;
json = WeatherDisasterWarningGrabber.getWeatherDisasterWarningJSON( cityCode );
//json = "{\"code\":\"200\",\"updateTime\":\"2022-03-17T15:46+08:00\",\"fxLink\":\"http://hfx.link/2qg5\",\"warning\":[{\"id\":\"10109110320220317152846295440963\",\"sender\":\"秦皇岛市气象台\",\"pubTime\":\"2022-03-17T15:28+08:00\",\"title\":\"秦皇岛市气象台更新大风蓝色预警[Ⅳ级/一般]\",\"status\":\"update\",\"level\":\"蓝色\",\"type\":\"1006\",\"typeName\":\"大风\",\"text\":\"秦皇岛市气象台2022年03月17日15时26分继续发布大风蓝色预警信号预计今天下午到夜间我市沿岸海域和沿海地区仍有东北风56级阵风78级其他地区45级阵风6级请防范。\",\"related\":\"10109110320220315152346034785400\",\"urgency\":\"\",\"certainty\":\"\"},{\"id\":\"10109110320220317111800391536363\",\"sender\":\"河北省气象台\",\"pubTime\":\"2022-03-17T11:18+08:00\",\"title\":\"河北省气象台更新寒潮黄色预警[Ⅲ级/较重]\",\"status\":\"update\",\"level\":\"黄色\",\"type\":\"1005\",\"typeName\":\"寒潮\",\"text\":\"河北省气象台2022年03月17日11时17分继续发布寒潮黄色预警信号受冷空气影响目前全省大部分地区最低气温已下降68℃局地下降812℃预计今天夜间到19日全省仍有明显降温过程大部分地区最低气温继续下降35℃西北部地区局地下降8℃以上18日到19日早晨北部地区最低气温降至-15-5℃其中坝上地区降至-20-15℃其他地区降至-50℃。请防范\",\"related\":\"10109110320220316110012116815176\",\"urgency\":\"\",\"certainty\":\"\"},{\"id\":\"10109110320220316154314834852572\",\"sender\":\"昌黎县气象台\",\"pubTime\":\"2022-03-16T15:43+08:00\",\"title\":\"昌黎县气象台更新寒潮蓝色预警[Ⅳ级/一般]\",\"status\":\"update\",\"level\":\"蓝色\",\"type\":\"1005\",\"typeName\":\"寒潮\",\"text\":\"昌黎县气象台2022年03月16日15时39分继续发布寒潮蓝色预警信号受冷空气影响预计今天下午到18日我县平均气温将继续下降6-8℃17日白天最高气温为3℃18日早晨最低气温可达-5℃请防范。\",\"related\":\"10109110320220315155452789394523\",\"urgency\":\"\",\"certainty\":\"\"}],\"refer\":{\"sources\":[\"12379\"],\"license\":[\"no commercial use\"]}}";
WeatherDisasterWarningGrabber.getWeatherDisasterWarningJSON( cityCode ); if ( json.isEmpty() == true )
{
System.out.println( "获取天气信息失败!" );
}
try
{
warning = WeatherDisasterWarningGrabber.convertWeatherDisasterWarning( json );
}
catch ( IOException error )
{
error.printStackTrace();
}
System.out.println( "1111" ); System.out.println( "1111" );
} }

View File

@ -6,17 +6,19 @@ public class QWeatherDisasterWarning
{ {
public QWeatherDisasterWarning( String code, public QWeatherDisasterWarning( String code,
String updateTime, String updateTime,
String flLink, String fxLink,
Vector<QWeatherDisasterWarningItem> warning, Vector<QWeatherDisasterWarningItem> warning,
QWeatherDisasterWarningRefer refer ) QWeatherDisasterWarningRefer refer )
{ {
this.code = code; this.code = code;
this.updateTime = updateTime; this.updateTime = updateTime;
this.flLink = flLink; this.fxLink = fxLink;
this.warning = warning; this.warning = warning;
this.refer = refer; this.refer = refer;
} }
public QWeatherDisasterWarning() {}
public String getCode() public String getCode()
{ {
return code; return code;
@ -37,14 +39,14 @@ public class QWeatherDisasterWarning
this.updateTime = updateTime; this.updateTime = updateTime;
} }
public String getFlLink() public String getFxLink()
{ {
return flLink; return fxLink;
} }
public void setFlLink( String flLink ) public void setFxLink( String fxLink )
{ {
this.flLink = flLink; this.fxLink = fxLink;
} }
public Vector<QWeatherDisasterWarningItem> getWarning() public Vector<QWeatherDisasterWarningItem> getWarning()
@ -69,7 +71,7 @@ public class QWeatherDisasterWarning
private String code; private String code;
private String updateTime; private String updateTime;
private String flLink; private String fxLink;
private Vector<QWeatherDisasterWarningItem> warning; private Vector<QWeatherDisasterWarningItem> warning;
private QWeatherDisasterWarningRefer refer; private QWeatherDisasterWarningRefer refer;
} }

View File

@ -2,8 +2,12 @@ package com.cpic.xim.disaster_warning;
import java.util.Objects; import java.util.Objects;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
public class QWeatherDisasterWarningItem public class QWeatherDisasterWarningItem
{ {
public QWeatherDisasterWarningItem() {}
public QWeatherDisasterWarningItem( String id, public QWeatherDisasterWarningItem( String id,
String sender, String sender,
String pubTime, String pubTime,
@ -14,7 +18,7 @@ public class QWeatherDisasterWarningItem
String typeName, String typeName,
String text, String text,
String related, String related,
String urgency, String centainy ) String urgency, String certainty )
{ {
this.id = id; this.id = id;
this.sender = sender; this.sender = sender;
@ -27,7 +31,7 @@ public class QWeatherDisasterWarningItem
this.text = text; this.text = text;
this.related = related; this.related = related;
this.urgency = urgency; this.urgency = urgency;
this.centainy = centainy; this.certainty = certainty;
} }
@Override @Override
@ -51,8 +55,8 @@ public class QWeatherDisasterWarningItem
that.type ) && Objects.equals( typeName, that.typeName ) && Objects.equals( text, that.type ) && Objects.equals( typeName, that.typeName ) && Objects.equals( text,
that.text ) && Objects.equals( that.text ) && Objects.equals(
related, related,
that.related ) && Objects.equals( urgency, that.urgency ) && Objects.equals( centainy, that.related ) && Objects.equals( urgency, that.urgency ) && Objects.equals( certainty,
that.centainy ); that.certainty );
} }
@Override @Override
@ -69,7 +73,7 @@ public class QWeatherDisasterWarningItem
text, text,
related, related,
urgency, urgency,
centainy ); certainty );
} }
public String getId() public String getId()
@ -182,18 +186,19 @@ public class QWeatherDisasterWarningItem
this.urgency = urgency; this.urgency = urgency;
} }
public String getCentainy() public String getCertainty()
{ {
return centainy; return certainty;
} }
public void setCentainy( String centainy ) public void setCertainty( String certainty )
{ {
this.centainy = centainy; this.certainty = certainty;
} }
private String id; private String id;
private String sender; private String sender;
@JsonFormat(pattern="yyyy-MM-dd'T'HH:mm")
private String pubTime; private String pubTime;
private String title; private String title;
private String status; private String status;
@ -203,5 +208,5 @@ public class QWeatherDisasterWarningItem
private String text; private String text;
private String related; private String related;
private String urgency; private String urgency;
private String centainy; private String certainty;
} }

View File

@ -4,53 +4,28 @@ import java.util.Objects;
public class QWeatherDisasterWarningRefer public class QWeatherDisasterWarningRefer
{ {
public QWeatherDisasterWarningRefer( String source, String license ) public QWeatherDisasterWarningRefer() {}
public String[] getSources()
{ {
this.source = source; return sources;
this.license = license;
} }
public String getSource() public void setSources( String[] sources )
{ {
return source; this.sources = sources;
} }
public void setSource( String source ) public String[] getLicense()
{
this.source = source;
}
public String getLicense()
{ {
return license; return license;
} }
public void setLicense( String license ) public void setLicense( String[] license )
{ {
this.license = license; this.license = license;
} }
@Override private String[] sources;
public boolean equals( Object o ) private String[] license;
{
if ( this == o )
{
return true;
}
if ( o == null || getClass() != o.getClass() )
{
return false;
}
QWeatherDisasterWarningRefer that = (QWeatherDisasterWarningRefer) o;
return source.equals( that.source ) && license.equals( that.license );
}
@Override
public int hashCode()
{
return Objects.hash( source, license );
}
private String source;
private String license;
} }

View File

@ -1,5 +1,7 @@
package com.cpic.xim.disaster_warning; package com.cpic.xim.disaster_warning;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.InputStreamReader; import java.io.InputStreamReader;
@ -107,4 +109,12 @@ public class WeatherDisasterWarningGrabber
return json; return json;
} }
public static QWeatherDisasterWarning convertWeatherDisasterWarning( String json )
throws IOException
{
ObjectMapper mapper = new ObjectMapper();
QWeatherDisasterWarning warning = mapper.readValue( json, QWeatherDisasterWarning.class );
return warning;
}
} }