我有一个POJO课程如下。
public class Client {
private static SecretKeySpec AES_Key;
private static final String key = "1234567890ABCDEF";
public static void main(String[] args) throws IOException, NoSuchAlgorithmException, NoSuchProviderException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
Socket mySocket = null;
PrintWriter out = null;
BufferedReader in = null;
AES_Key = new SecretKeySpec(key.getBytes(), "AES");
System.out.println(AES_Key);
Cipher AES_Cipher = Cipher.getInstance("AES/ECB/PKCS5Padding", "BC");
try {
mySocket = new Socket("localhost", 4443);
out = new PrintWriter(mySocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(mySocket.getInputStream()));
} catch (UnknownHostException e) {
System.err.println("Don't know about host");
System.exit(1);
} catch (IOException e) {
System.err.println("Couldn't get I/O for the connection to: localhost.");
System.exit(1);
}
BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
String fromServer;
String fromUser;
System.out.println("Client run ");
while (true) {
System.out.println("type message :");
AES_Cipher.init(Cipher.ENCRYPT_MODE, AES_Key);
fromUser = stdIn.readLine();
byte plaintext[] = fromUser.getBytes();
byte final_plaintext[] = AES_Cipher.doFinal(plaintext);
// fromUser=toHexString(final_plaintext);
String msg = new String(final_plaintext, "ASCII");
System.out.println(final_plaintext);
if (fromUser != null) {
out.println(msg);
}
else{ break; }
fromServer = in.readLine();
if(fromServer!=null){
System.out.println("Client receive :" + fromServer);
}
else{ break; }
}
out.close();
in.close();
stdIn.close();
mySocket.close();
}
private static String toHexString(byte[] block) {
StringBuffer buf = new StringBuffer();
int len = block.length;
for (int i = 0; i < len; i++) {
byte2hex(block[i], buf);
if (i < len - 1) {
buf.append(":");
}
}
return buf.toString();
}
/*
* Converts a byte to hex digit and writes to the supplied buffer
*/
private static void byte2hex(byte b, StringBuffer buf) {
char[] hexChars = {'0', '1', '2', '3', '4', '5', '6', '7', '8',
'9', 'A', 'B', 'C', 'D', 'E', 'F'};
int high = ((b & 0xf0) >> 4);
int low = (b & 0x0f);
buf.append(hexChars[high]);
buf.append(hexChars[low]);
}
}
所有JSON对象内部,只有一个地理空间属性具有纬度和经度,这是一个不同的POJO类。使用import java.util.List;
public class LocationInfoDTO{
private int _id;
private String name;
private String type;
private String fullName;
private int location_id;
private boolean isEurope;
private String countryCode;
private boolean coreCountry;
private int iataAirportCode;
private List<Geospatial> geo_location;
public int get_id() {
return _id;
}
public void set_id(int _id) {
this._id = _id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getFullName() {
return fullName;
}
public void setFullName(String fullName) {
this.fullName = fullName;
}
public int getLocation_id() {
return location_id;
}
public void setLocation_id(int location_id) {
this.location_id = location_id;
}
public boolean isEurope() {
return isEurope;
}
public void setEurope(boolean isEurope) {
this.isEurope = isEurope;
}
public String getCountryCode() {
return countryCode;
}
public void setCountryCode(String countryCode) {
this.countryCode = countryCode;
}
public boolean isCoreCountry() {
return coreCountry;
}
public void setCoreCountry(boolean coreCountry) {
this.coreCountry = coreCountry;
}
public int getIataAirportCode() {
return iataAirportCode;
}
public void getIataAirportCode(int iataAirportCode) {
this.iataAirportCode = iataAirportCode;
}
@Override
public String toString() {
return "LocationInfoDTO [_id=" + _id + ", name=" + name + ", type="
+ type + ", fullName=" + fullName + ", location_id="
+ location_id + ", isEurope=" + isEurope + ", countryCode="
+ countryCode + ", coreCountry=" + coreCountry
+ ", iataAirportCode=" + iataAirportCode + ", geo_location="
+ geo_location + "]";
}
public List<Geospatial> getGeo_location() {
return geo_location;
}
public void setGeo_location(List<Geospatial> geo_location) {
this.geo_location = geo_location;
}
}
GSON
除了{{1} }是fromJSON
;
geo-location
这是一个示例JSON。
以这种方式触发
null
EDIT --------------------------------
将{"_id":376217,"key":null,"name":"test","fullName":"test, test","geo_position":{"latitude":52.52437,"longitude":13.41053}}
更改为GsonBuilder gsonBuilder = new GsonBuilder();
Gson gson = gsonBuilder.create();
JSONArray array = new JSONArray(json);
for(int i=0;i<array.length();i++){
System.out.println("Array:"+gson.fromJson(array.getJSONObject(i).toString(), LocationInfoDTO.class));
geo-location
答案 0 :(得分:0)
错误说exaclty出了什么问题。你有geo-location
的json对象,它需要一个列表。即使它是一个元素列表。改变
{"_id":376217,"key":null,"name":"test","fullName":"test, test","geo_position":{"latitude":52.52437,"longitude":13.41053}}
到
{"_id":376217,"key":null,"name":"test","fullName":"test, test","geo_position":[{"latitude":52.52437,"longitude":13.41053}]}
<强>更新强>
由于总会有一个纬度/经度,您应该更新您的POJO,以便geo_location
是一个GeoSpatial
对象,而不是List<GeoSpatial>