나는 데이터 클래스 서버 복귀를 변환 GSON를 사용합니다. 그리고 난 같은 데이터 클래스를 정의 ... 그리고 내가 주석으로 ExclusionStrategy를 추가 그래서 .. 내가 GSON로 변환 할하지 않는 일부 필드를 프로그래머. 시험을 통해, 내가 정상적으로 작동이 AnnotationExclusionStrategy을 발견, 그리고 난 변환하고 싶지 않은 필드를 건너 뜁니다. 나는 uiType 및 testdata로 같은 분야에 주석을 추가 할 수 있지만 실제로 내가 개체를 인쇄 할 때. 나는 필드 uiType을 발견하고 testdata로는 제로 null로 변환했다 ..
그리고 난 새로운 데이터 클래스를 만들 수 있습니다. uiType 및 testdata로 0으로 삭제하지 않았거나 null.Is 때 GSON 변환 잘못?
이 문제를 해결하기 위해 어떤 방법이 있는지?
여기에 코드입니다.
데이터 클래스 엔티티 :
data class Product(@SerializedName("num_iid") var productId: Long,
@SerializedName("title") var productName: String,
@SerializedName("item_url") var productUrl: String,
@SerializedName("pict_url") var productPic: String,
@SerializedName("small_images") var productPics: ProductImageList,
@SerializedName("zk_final_price") var productPrice: String,
@SerializedName("reserve_price") var originPrice: String,
@SerializedName("provcity") var place: String,
@SerializedName("volume") var sellCount: String,
@SerializedName("user_type") var sellerType: String,
@SerializedName("nick") var sellerName: String,
@SerializedName("seller_id") var sellerId: Long
) : MultiItemEntity {
@Exclude
@ShoppingCenterConstant.ProductListUiType
var uiType = ShoppingCenterConstant.LIST_UI_TYPE_COMMON
var testData = "TestData"
override fun getItemType(): Int {
return ShoppingCenterConstant.LIST_UI_TYPE_COMMON
}
override fun toString(): String {
return "Product(productId=$productId, productName='$productName', productUrl='$productUrl', productPic='$productPic', productPics=$productPics, productPrice='$productPrice', originPrice='$originPrice', place='$place', sellCount='$sellCount', sellerType='$sellerType', sellerName='$sellerName', sellerId=$sellerId, uiType=$uiType, testData='$testData')"
}
}
GSON의 AnnotationExclusion :
public class AnnotationExclusion implements ExclusionStrategy {
public static final String TAG = GsonUtil.TAG;
@Override
public boolean shouldSkipField(FieldAttributes f) {
boolean isShouldSkip = f.getAnnotation(Exclude.class) != null;
return isShouldSkip;
}
@Override
public boolean shouldSkipClass(Class<?> clazz) {
boolean isShouldSkip = clazz.getAnnotation(Exclude.class) != null;
return isShouldSkip;
}
}
내가 인쇄 출력 할 장소 :
override fun showData(data: BaseListBean<Product>) {
var product = Product(0, "", "", ""
, ProductImageList(List(0, { String() })), "", "", "", ""
, "", "", 0)
LogUtils.info("print out new product:" + product)
mResultListener.onResponse(data)
}
GSON 변환기와 결과 , 당신은 필드 uiType 및 testdata로 0 또는 null로 변환했다 볼 수 있습니다 :
Product(productId=552634381786, productName='eoodoo新生儿礼盒婴儿衣服秋冬套装出生满月礼物初生母婴宝宝用品', productUrl='http://item.taobao.com/item.htm?id=552634381786', productPic='https://img.alicdn.com/tfscom/i4/2832303009/O1CN013tXM6L1Y6AFHSy7xN_!!0-item_pic.jpg', productPics=com.laka.ergou.mvp.shopping.center.model.bean.ProductImageList@1033f68, productPrice='259.72', originPrice='755.00', place='浙江 杭州', sellCount='1159', sellerType='1', sellerName='eoodoo旗舰店', sellerId=2832303009, uiType=0, testData='null')
새로운 객체를 생성과 결과 , 이 좋은 것 같다 :
Product(productId=0, productName='', productUrl='', productPic='', productPics=com.laka.ergou.mvp.shopping.center.model.bean.ProductImageList@1c228b, productPrice='', originPrice='', place='', sellCount='', sellerType='', sellerName='', sellerId=0, uiType=1, testData='testData')
여기에 사용 된 인 JSON 나누었다 uiType TestData 사용과 같은 필드가. 내가 코드에서 정의하고 현지에서 사용되는 바로 필드,이 서버의 컴백 JSON 필드 아니다
{
"tbk_item_get_response":{
"results":{
"n_tbk_item":[
{
"item_url":"http://item.taobao.com/item.htm?id=582889134126",
"nick":"宝绒羊服饰旗舰店",
"num_iid":582889134126,
"pict_url":"https://img.alicdn.com/tfscom/i1/1720028843/O1CN01ERqlHz2FC8tL1m2tz_!!0-item_pic.jpg",
"provcity":"浙江 杭州",
"reserve_price":"1318.00",
"seller_id":1720028843,
"small_images":{
"string":[
"https://img.alicdn.com/tfscom/i4/1720028843/O1CN01xD9JyD2FC8tJMRuk2_!!1720028843.jpg",
"https://img.alicdn.com/tfscom/i4/1720028843/O1CN01twbf3O2FC8tLEHVJc_!!1720028843.jpg",
"https://img.alicdn.com/tfscom/i4/1720028843/O1CN01zM2clN2FC8tE0LO5q_!!1720028843.jpg",
"https://img.alicdn.com/tfscom/i3/1720028843/O1CN01WW6xzz2FC8tKjoitz_!!1720028843.jpg"
]
},
"title":"羽绒服女2018新款女装冬季短款韩版时尚银色亮面滩羊毛领加厚外套",
"user_type":1,
"volume":2138,
"zk_final_price":"498.00"
}
]
},
"total_results":19643,
"request_id":"kvbqd1jrgqcg"
}
}
GSON 현재 코 틀린 유형 안전을 파괴한다. 그것은 null이 아닌 속성에 널 (null)을 작성할 수 있습니다. 당신이 null로 할당 된 해당 필드를 읽는 것을 할 때 JSON 즉 발생할 수 있습니다. 예를 들어, 당신은 데이터 클래스가있는 경우 :
data class A (
var nonNullable: String = "",
var other: Int = 42
)
그리고 JSON을 다음 :
{
"nonNullable": null,
"other": 91
}
당신은 것 null
의 nonNullable
결과로.
당신이 당신의 클래스에 대한 정의 GSON의 형식 변환기를 만들거나 만들 수 있다는 것을 피하기 위해, nonNullable
무시하는, getter 및 setter를 실제로 널 (NULL)하지만 제공하는 null
가치를 :
class A {
var nonNullable: String? = ""
get() = field ?: ""
set(value) {
if (value != null) {
field = value
}
}
}
그리고 같은 일 0 - 수단 JSON 당신이 제로에 할당 된 해당 필드를 구문 분석하는 것이 있다는 것을. 또는, 그 그렇게하지 않을 경우 - 더 명확하게 질문에 저희에게 당신의 JSON을 제공합니다.