json转java对象
google提供的Gson使json直接转对应的java类非常好用,但前提是该json的属性类型,结构不变才行,若是那种属性一会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 li.yanlong@icjhd.com * * @date 2022-4-1 19:07:17 */ @Component @Slf4j public class IOUtil { /** * <pre> * 读取文件内容 * </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> * 传入文件内容,将内容转换成指定的类型并返回。支持的类型为:{A:{}, A:{}, A{}}, 此类型为在一个结构体中拥有很多小结构体,而这些小结构体的结构又相同,但他们并不是数组类型,所以专本用此方法来解析。 * </pre> * * @param <T> 需要解析的结构体 * @param content 整个json文件的内容 * @param keys 所有结构体的变量名 * @param classOfT 结构体类型 * @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("结果abiliety3:{}", abilety3); // String data = jsonObject.getString(key); // if (!StringUtils.isNotBlank(data)) { // continue; // } //方式二json转对象,对象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 英雄模型数据 * * @author li.yanlong@icjhd.com * * @date 2022-4-2 9:38:27 */ @Slf4j @Data public class HeroModel implements Serializable{ private static final long serialVersionUID = -5785153848105380905L; /** 职业 */ @SerializedName(value = "UnitVocation") private String unitVocation; /** 基础生命值 */ @SerializedName(value = "StatusHealth") private Integer statusHealth; /** 成长生命值 */ @SerializedName(value = "StatusHealthUp") private Integer statusHealthUp; /** 自动恢复生命时是否发生在停止攻击后 */ @SerializedName(value = "IsAutoHealthRegen") private Integer isAutoHealthRegen; /** 基础生命恢复延迟时间 */ @SerializedName(value = "StatusHealthRegenDelayTime") private Integer statusHealthRegenDelayTime; /** 基础生命恢复间隔时间 */ @SerializedName(value = "StatusHealthRegenTimeInterval") private Integer statusHealthRegenTimeInterval; /** 基础生命恢复百分比 */ @SerializedName(value = "StatusHealthRegen") private Integer statusHealthRegen; /** 基础弹夹值 */ @SerializedName(value = "StatusMana") private Integer statusMana; /** 弹夹恢复时间 */ @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; /** 基础伤害 */ @SerializedName(value = "DamageBase") private Integer damageBase; /** 成长伤害 */ @SerializedName(value = "DamageUp") private Integer damageUp; /** 特技基础伤害 */ @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; }
本文来自博客园,作者:~~mr.li~~,转载请注明原文链接:https://www.cnblogs.com/li-yan-long/p/16227818.html