前提:近期使用高德一个接口,上了生产,才发现json里面的list实体,数据格式不一致。有数据的时候,数据为String类型,无数据的时候,数据为“[]”.(如图)
geocodes这个值里面,第一个list是正常的值,第二个list是个空值,但是前面有数据的定义为string,而第二个list定义为[]。那么在json转bean的时候,就会出现(图2)情况。
json转bean的时候,自动填充为值为“[]”。这样的数据保留是不满足业务需要的。
所以,可以制定一个注解,String类型的"[]"填充为 空字符串“”。List类型的属性就不需要理会。
代码如下:


实体:
package com.test.gaode003;
import lombok.Getter;
import lombok.Setter;
import java.util.List;
@Getter
@Setter
public class GaoDeEntity {
private static final long serialVersionUID = -4506875151642772406L;
private String status;
private String info;
private String infocode;
private String count;
private List<GeocodesEntity> geocodes;
}
package com.test.gaode003;
import lombok.Getter;
import lombok.Setter;
import java.util.List;
public @Getter
@Setter
class GeocodesEntity {
private static final long serialVersionUID = 942508470576711519L;
@ReflectTag
private String formattedAddress;
@ReflectTag
private String country;
@ReflectTag
private String province;
@ReflectTag
private String citycode;
@ReflectTag
private String city;
@ReflectTag
private String district;
private List<String> township;
@ReflectTag
private NeighborhoodDomain neighborhood;
@ReflectTag
private BuildingDomain building;
@ReflectTag
private String adcode;
private List<String> street;
private List<String> number;
@ReflectTag
private String location;
@ReflectTag
private String level;
@Getter
@Setter
class NeighborhoodDomain {
private static final long serialVersionUID = 7216259952414552319L;
private List<String> name;
private List<String> type;
}
@Getter
@Setter
class BuildingDomain {
private static final long serialVersionUID = -4653848900240913579L;
private List<String> name;
private List<String> type;
}
}
自定义注解:
package com.test.gaode003;
import java.lang.annotation.*;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
@Documented
public @interface ReflectTag {
}
测试:
package com.test.gaode003;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.io.FileUtils;
import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;
public class Test {
@org.junit.Test
public void test() throws IOException {
String str = FileUtils.readFileToString(new File("C:\\workProject\\360\\test_data\\data\\test90.txt"), "UTF-8");
GaoDeEntity GaoDeEntity = JSONObject.parseObject(str, GaoDeEntity.class);
List<GeocodesEntity> geocodes = GaoDeEntity.getGeocodes();
for (GeocodesEntity geocode : geocodes) {
Class<? extends GeocodesEntity> clazz = geocode.getClass();
Field[] fields = clazz.getDeclaredFields();
for (Field field : fields) {
if (field.isAnnotationPresent(ReflectTag.class)){
PropertyDescriptor propertyDescriptor = null;
try {
propertyDescriptor = new PropertyDescriptor(field.getName(), clazz);
} catch (IntrospectionException e) {
e.printStackTrace();
}
Method writeMethod = propertyDescriptor.getWriteMethod();
Method readMethod = propertyDescriptor.getReadMethod();
Object value = null;
try {
value = readMethod.invoke(geocode);
String s = value.toString();
if ("[]".equals(s.trim())) {
writeMethod.invoke(geocode,"");
}
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
}
}
System.out.println("geocodes:"+ JSON.toJSONString(geocodes));
}
}
执行结果:String类型的都已经填充为"",List类型还是List类型
[{
"adcode": "320117",
"building": {
"name": [],
"type": []
},
"city": "南京市",
"citycode": "025",
"country": "中国",
"district": "溧水区",
"formattedAddress": "江苏省南京市溧水区永阳镇",
"level": "乡镇",
"location": "119.044331,31.659977",
"neighborhood": {
"name": ["南京市", "南京市", "南京市"],
"type": []
},
"number": [],
"province": "江苏省",
"street": [],
"township": []
}, {
"adcode": "",
"building": {
"name": [],
"type": []
},
"city": "",
"citycode": "",
"country": "",
"district": "",
"formattedAddress": "",
"level": "",
"location": "",
"neighborhood": {
"name": [],
"type": []
},
"number": [],
"province": "",
"street": [],
"township": []
}]
原始报文:test90.txt
{
"status": "1",
"info": "OK",
"infocode": "10000",
"count": "2",
"geocodes": [
{
"formatted_address": "江苏省南京市溧水区永阳镇",
"country": "中国",
"province": "江苏省",
"citycode": "025",
"city": "南京市",
"district": "溧水区",
"township": [],
"neighborhood": {
"name": ["南京市","南京市","南京市"],
"type": []
},
"building": {
"name": [],
"type": []
},
"adcode": "320117",
"street": [],
"number": [],
"location": "119.044331,31.659977",
"level": "乡镇"
},
{
"formatted_address": [],
"country": [],
"province": [],
"citycode": [],
"city": [],
"district": [],
"township": [],
"neighborhood": {
"name": [],
"type": []
},
"building": {
"name": [],
"type": []
},
"adcode": [],
"street": [],
"number": [],
"location": [],
"level": []
}
]
}

该博客讲述了在使用高德地图接口时遇到的一个JSON数据格式问题,即当list实体为空时,数据以[]形式存在,导致在JSON转Java Bean过程中出现问题。博主通过自定义注解`ReflectTag`和相关代码处理,实现了将String类型的[]转换为空字符串,但List类型保持不变。测试结果显示,String类型的字段已按预期填充,但List类型字段未做相应处理。
1318

被折叠的 条评论
为什么被折叠?



