好久没提问了,json取值问题

{"Status":0,"Key":"aaa","Data":[{"Name":"安娜","Type":5,"EName":"ANAA","Spell":"ANNA","ShortSpell":"AN","Country":"法属波利尼西亚","CountryCode":"PF","Code":"AAA","CityId":7500,"TimeZone":"-600","POIID":115243},{"Name":"安娜机场","Type":3,"EName":"ANAA AIRPORT","Spell":"ANNAJICHANG","ShortSpell":"ANJC","Country":"法属波利尼西亚","CountryCode":"PF","Code":"AAA","Datas":[{"Name":"安娜","Type":5,"EName":"ANAA","Country":"法属波利尼西亚","CountryCode":"PF","Code":"AAA","CityId":7500,"TimeZone":"-600","POIID":115243}],"POIID":116761}]}

获取到的json,怎么按想要的字段取值,存入数据库中

评论区

JFinal

2019-10-17 11:17

需要先转成 Model,并且该 Model 是通过生成器生成了 setter 方法的 ,假定 Model 名是 Article 可以通过如下代码存入数据库:
Article article = FastJson.getJson().parse(jsonString, Article.class);
article.save();

如果 jsonString 中的数据与 Article 的对应关系并不是严格对应字段的,可以通过 Map 作为中间转换:
Map map = FastJson.getJson().parse(jsonString, Map.class);
Article article = new Article();
article.setXxx(map.get(xxx));
article.setYyy(map.get(yyy));
article.save();

热门反馈

扫码入社