gpt4 book ai didi

java - 如何使用Gson将JSON转换为具有自定义字段名称的POJO

转载 作者:行者123 更新时间:2023-11-30 07:46:14 25 4
gpt4 key购买 nike

我最近开始使用 GSON,并且在反序列化此 json 时遇到问题。问题出在下面的 json 元素,该元素在字段名称中包含空格

{
longDescriptionNonHTML: {
What it is:: " An oil-free, color-tinted moisturizer."
What it does:: " This lightweight foundation can be reapplied as necessary and offers moisturizing, oil-control coverage for sensitive or acne prone skin."
}

{
SKUType: "restricted"
brandName: "Laura Mercier"
brandId: 5809
skuID: "1228139"
availableInStore: 0
topSellerRank: 8297
productName: "Tinted Moisturizer - Oil Free"
shade_description: "Porcelain"
shortDescription: "What it is: An oil-free, color-tinted moisturizer.What it does: This lightweight foundation can be reapplied as necessary and offers moisturizing, oil-control coverage for sensitive or acne prone skin."
size: "1.7 oz"
listPrice: 0
salePrice: 0
image: "http://www.sephora.com/productimages/sku/s1228139-main-hero.jpg"
rating_product: 4.3
online_store: "http://www.sephora.com/tinted-moisturizer-oil-free-P310929?skuId=1228139&lang=en"
imageBrand: "http://www.sephora.com/contentimages/brands/lauramercier/5809_logo_279.png"
productId: "P310929"
formulation: "Liquid"
spf: ""
coverage: "Sheer, Medium"
finish: "Matte, Natural"
ingredients: "Vitamin C"
skintype: "Combination, Normal, Oily"
longDescription: "<b>What it is:</b><br> An oil-free, color-tinted moisturizer.<br><br><b>What it does:</b><br> This lightweight foundation can be reapplied as necessary and offers moisturizing, oil-control coverage for sensitive or acne prone skin."
longDescriptionNonHTML: {
What it is:: " An oil-free, color-tinted moisturizer."
What it does:: " This lightweight foundation can be reapplied as necessary and offers moisturizing, oil-control coverage for sensitive or acne prone skin."
}-
skintone: "2Y03"
longIngredientsDesc: ""
language: "en"
isPrimarySkintone: 1
isDefaultSku: 0
storeonly: 0
}

我在POJO下面写了

公共(public)类 LongDescriptionNonHTML {

@SerializedName("What it is:")
private String whatItIs;
@SerializedName("What it does:")
private String whatItDoes;

public LongDescriptionNonHTML(String a,String b){
whatItDoes=a;
whatItDoes=b;
}

public String getWhatItIs() {
return whatItIs;
}

public void setWhatItIs(String whatItIs) {
this.whatItIs = whatItIs;
}

public String getWhatItDoes() {
return whatItDoes;
}

public void setWhatItDoes(String whatItDoes) {
this.whatItDoes = whatItDoes;
}

}

为了反序列化它,我写了下面的代码

Gson gson = new GsonBuilder().setPrettyPrinting().create();
JsonReader reader = new JsonReader(new FileReader(fileName));
LongDescriptionNonHTML obj=gson.fromJson(reader, LongDescriptionNonHTML.class);
System.out.println(obj.getWhatItDoes());

但它正在打印空值。

最佳答案

将 JSON 转换为 POJO 类可能对您有用。

public static <T> T convertJSON2POJOClass(String json, Class<T> type) 
{
GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.serializeNulls();

try{
return (T) gson.fromJson(json, type);
} catch (Exception expJSONToClassConvertor) {
baseDAO.getInstance().logAnError("common", baseDAO.getInstance().stackTraceToString(expJSONToClassConvertor));
return null;
}

关于java - 如何使用Gson将JSON转换为具有自定义字段名称的POJO,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33910498/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com