读取本地JSON文件为Resource资源对象
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.Resource;
@Value("classpath:/xxxx.json")
private Resource jsonFile;
将JavaBean中驼峰字段名转换成json时,字段大写变成下划线加小写的形式
如字段 userName 对应的json中的字段应该为user_name
import com.alibaba.fastjson.serializer.SerializeConfig;
import com.alibaba.fastjson.JSON;
// userSecretVo 这个为一个JavaBean对象
SerializeConfig config = new SerializeConfig();
config.setPropertyNamingStrategy(PropertyNamingStrategy.SnakeCase);
String jsonString = JSON.toJSONString(userSecretVo, config);
上传json文件,解析后,转换对应的实体
@PostMapping("/upload")
public R upload(@RequestParam("file") MultipartFile file) {
// UserSecretEntity 该实体的字段,与json文件中的字段相对应
JSONObject jsonObject = JSON.parseObject(new String(file.getBytes()));
UserSecretEntity userSecretEntity = JSON.parseObject(jsonObject.toJSONString(), new TypeReference<UserSecretEntity>() {
});
}