更新時間:2022-10-19 10:47:28 來源:動力節(jié)點 瀏覽1232次
google提供的Gson使json直接轉(zhuǎn)對應(yīng)的java類非常好用,但前提是該json的屬性類型,結(jié)構(gòu)不變才行,若是那種屬性一會int一會String的情況,那么將不適用于該方式,只能通過alibaba的JSONObject一個一個按照屬性名取。
package com.cjhd.fruit.database.util;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.List;
import java.util.Map;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Maps;
import com.google.gson.Gson;
import com.google.gson.annotations.SerializedName;
import lombok.extern.slf4j.Slf4j;
/**
*
* @describe 對本地文件的一些操作
*
* @author [email protected]
*
* @date 2022-4-1 19:07:17
*/
@Component
@Slf4j
public class IOUtil {
/**
* <pre>
* 讀取文件內(nèi)容
* </pre>
*
* @param folderName 位于resource目錄下的文件夾名稱,如:static, static/map
* @param fileName 文件名稱,如:resource/static/hero.json 則直接傳 /hero.json
* @return
*/
public String readFileContent(String folderName, String fileName) {
URL url = this.getClass().getProtectionDomain().getClassLoader().getResource(folderName);
if (url == null) {
log.error("需要找的目錄 static/hero 在資源文件夾下找不到");
return null;
}
File file = new File(url.getPath() + fileName);
String content = "";
try {
content = FileUtils.readFileToString(file, "UTF-8");
} catch (IOException e) {
e.printStackTrace();
}
return content;
}
/**
* <pre>
* 傳入文件內(nèi)容,將內(nèi)容轉(zhuǎn)換成指定的類型并返回。支持的類型為:{A:{}, A:{}, A{}}, 此類型為在一個結(jié)構(gòu)體中擁有很多小結(jié)構(gòu)體,而這些小結(jié)構(gòu)體的結(jié)構(gòu)又相同,但他們并不是數(shù)組類型,所以專本用此方法來解析。
* </pre>
*
* @param <T> 需要解析的結(jié)構(gòu)體
* @param content 整個json文件的內(nèi)容
* @param keys 所有結(jié)構(gòu)體的變量名
* @param classOfT 結(jié)構(gòu)體類型
* @return
*/
public static <T> Map<String, T> fromJson(String content, List<String> keys, Class<T> classOfT) {
Map<String, T> map = Maps.newHashMap();
JSONObject jsonObject = JSONObject.parseObject(content);
for (String key : keys) {
if (!jsonObject.containsKey(key)) {
continue;
}
JSONObject jsonObj = jsonObject.getJSONObject(key);
Integer abilety3 = jsonObj.getInteger("Ability3");//方式一按屬性名取。
boolean is3Array = JSONObject.isValidArray(jsonObj.getString("Ability3"));
boolean is4Array = JSONObject.isValidArray(jsonObj.getString("Ability4"));
jsonObj.getJSONArray("Ability4");
log.info("結(jié)果abiliety3:{}", abilety3);
// String data = jsonObject.getString(key);
// if (!StringUtils.isNotBlank(data)) {
// continue;
// }
//方式二json轉(zhuǎn)對象,對象T中的屬性若加@SerializedName(value = "json變量名")注解,可單獨制定映射的名稱,就可以不用屬性名和json變量名一致了。
// T obj = (T) new Gson().fromJson(data, classOfT);
// if (obj == null) {
// continue;
// }
// map.put(key, obj);
}
return map;
}
}
package com.cjhd.fruit.ai.core;
import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import com.google.gson.annotations.SerializedName;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
/**
*
* @describe 英雄模型數(shù)據(jù)
*
* @author [email protected]
*
* @date 2022-4-2 9:38:27
*/
@Slf4j
@Data
public class HeroModel implements Serializable{
private static final long serialVersionUID = -5785153848105380905L;
/** 職業(yè) */
@SerializedName(value = "UnitVocation")
private String unitVocation;
/** 基礎(chǔ)生命值 */
@SerializedName(value = "StatusHealth")
private Integer statusHealth;
/** 成長生命值 */
@SerializedName(value = "StatusHealthUp")
private Integer statusHealthUp;
/** 自動恢復(fù)生命時是否發(fā)生在停止攻擊后 */
@SerializedName(value = "IsAutoHealthRegen")
private Integer isAutoHealthRegen;
/** 基礎(chǔ)生命恢復(fù)延遲時間 */
@SerializedName(value = "StatusHealthRegenDelayTime")
private Integer statusHealthRegenDelayTime;
/** 基礎(chǔ)生命恢復(fù)間隔時間 */
@SerializedName(value = "StatusHealthRegenTimeInterval")
private Integer statusHealthRegenTimeInterval;
/** 基礎(chǔ)生命恢復(fù)百分比 */
@SerializedName(value = "StatusHealthRegen")
private Integer statusHealthRegen;
/** 基礎(chǔ)彈夾值 */
@SerializedName(value = "StatusMana")
private Integer statusMana;
/** 彈夾恢復(fù)時間 */
@SerializedName(value = "StatusManaRegen")
private Integer statusManaRegen;
/** 能量上限 */
@SerializedName(value = "EnergyLimit")
private Integer energyLimit;
/** 擊中充能值 */
@SerializedName(value = "EnergyPerHit")
private Integer energyPerHit;
/** 每秒自動充能 */
@SerializedName(value = "EnergyPerSec")
private Integer energyPerSec;
/** 視野范圍格子 */
@SerializedName(value = "VisionRange")
private Integer visionRange;
/** 是否有空中視野 */
@SerializedName(value = "HasFlyHeightVisual")
private Integer hasFlyHeightVisual;
/** 攻擊能力 */
@SerializedName(value = "AttackCapabilities")
private String attackCapabilities;
/** 基礎(chǔ)傷害 */
@SerializedName(value = "DamageBase")
private Integer damageBase;
/** 成長傷害 */
@SerializedName(value = "DamageUp")
private Integer damageUp;
/** 特技基礎(chǔ)傷害 */
@SerializedName(value = "SpecialDamageBase")
private Integer specialDamageBase;
/** 特技成長傷害 */
@SerializedName(value = "SpecialDamageUp")
private Integer specialDamageUp;
/** 移動能力 */
@SerializedName(value = "MovementCapabilities")
private String movementCapabilities;
/** 移動速度/秒 */
@SerializedName(value = "MovementSpeed")
private Integer movementSpeed;
/** 普通技能 */
@SerializedName(value = "Ability1")
private Integer ability1;
/** 超級技能 */
@SerializedName(value = "Ability2")
private Integer ability2;
/** 妙力技能 */
@SerializedName(value = "Ability3")
private List<Integer> ability3;
/** 元氣技能 */
@SerializedName(value = "Ability4")
private List<Integer> ability4;
}
相關(guān)閱讀
初級 202925
初級 203221
初級 202629
初級 203743