我刚刚完成了为期6个月的编码速成课程的新手编码器。我正在研究一个java webapp来展示我的技能,而我所涉及的项目构思涉及从API中检索JSON数据,这是我们在课堂上没有学到的。我创建了POJO来匹配JSON,我正在尝试将JSON解析为java对象以存储在数据库中,但是当我运行应用程序时,我的数据库表永远不会填充数据。我怀疑问题是我的方法转换JSON,但任何反馈都非常感谢。这是我认为相关的所有代码,对不起,如果它的TMI。如果我的代码很难看,我也很抱歉,我是初学者......谢谢!
API返回JSON,如下所示:
{
"result":{
"status":1,
"num_results":1,
"total_results":500,
"results_remaining":499,
"matches":[{
"match_id":3188095188,
"match_seq_num":2784956606,
"start_time":1495079320,
"lobby_type":7,
"radiant_team_id":0,
"dire_team_id":0,
"players":[{
"account_id":86920222,
"player_slot":0,
"hero_id":18
},{
"account_id":61122568,
"player_slot":1,
"hero_id":85
},{
"account_id":10208661,
"player_slot":2,
"hero_id":13
},{
"account_id":106083675,
"player_slot":132,
"hero_id":50
}]
}]
}
}
我的POJO:
@Entity
@JsonIgnoreProperties(ignoreUnknown = true)
public class Result {
@JsonIgnore
@Id
@GeneratedValue
private int id;
@JsonProperty("status")
private int status;
@JsonProperty("num_results")
private int num_results;
@JsonProperty("total_results")
private int total_results;
@JsonProperty("results_remaining")
private int results_remaining;
@OneToMany
@JoinColumn(name = "result_id")
@ElementCollection(targetClass=Matches.class)
@JsonProperty("matches")
private List<Matches> matches;
// getters and setters
}
@Entity
@JsonIgnoreProperties(ignoreUnknown = true)
public class Matches {
@Id
@JsonProperty("match_id")
private int match_id;
@JsonIgnore
@ManyToOne
private Result result;
@JsonProperty("match_seq_num")
private int match_seq_num;
@JsonProperty("start_time")
private int start_time;
@JsonProperty("lobby_type")
private int lobby_type;
@JsonProperty("radiant_team_id")
private int radiant_team_id;
@JsonProperty("dire_team_id")
private int dire_team_id;
@OneToMany
@JoinColumn(name = "Matches_id")
@ElementCollection(targetClass=Players.class)
@JsonProperty("players")
private List<Players> players;
// getters and setters
}
@Entity
@JsonIgnoreProperties(ignoreUnknown = true)
public class Players {
@JsonIgnore
@Id
@GeneratedValue
private int id;
@JsonIgnore
@ManyToOne
private Matches matches;
@JsonProperty("account_id")
private int account_id;
@JsonProperty("player_slot")
private int player_slot;
@JsonProperty("hero_id")
private int hero_id;
// getters and setters
}
读取JSON并将其转换为对象的服务方法(url被审查,不希望我的API密钥公开)
public class SteamService {
public static Result getMatchHistory(String steamid){
Result result = new Result();
String MatchHistoryUrl = "https:**URL**="+steamid;
RestTemplate restTemplate = new RestTemplate();
Result jsonresult = restTemplate.getForObject(MatchHistoryUrl, Result.class);
return jsonresult;
}
}
控制器
@Controller
@RequestMapping("")
public class HomeController {
@Autowired
private ResultsDao resultsDao;
@RequestMapping(value = "", method = RequestMethod.GET)
public String index(Model model){
model.addAttribute("title", "Welcome");
return "home/home";
}
@RequestMapping(value = "", method = RequestMethod.POST)
public String processSteamIdField(@RequestParam("steamid")String steamid, Model model) {
Result newresult = getMatchHistory(steamid);
resultsDao.save(newresult);
return "redirect:results";
}
}
DAO
@Repository
@Transactional
public interface ResultsDao extends CrudRepository<Result, Integer>{
}
答案 0 :(得分:0)
也许我的方法有点天真,但是......如果你想将JSON作为字符串存储在数据库中,那么我会使用一个对象映射器:
new ObjectMapper().writeValueAsString(myObject);
并且为了阅读JSON并将其解析为我将要执行的类:
new ObjectMapper().readValue(JSON_STRING_HERE, "utf-8"), MyPOJO.class);
此外,如果您已经使用Spring,那么您的控制器可能看起来像这样(例如,对于POST)
@RequestMapping(value = "/", method = RequestMethod.POST)
public MyPojo myController(@RequestBody MyPojo myBody) {
myRepository.save(myBody);
}
因此,解析客户端发送到您的应用程序和控制器的JSON已经由Spring处理