Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/368.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法将JSON从URL读取到java对象/存储在mysql数据库中_Java_Mysql_Json - Fatal编程技术网

无法将JSON从URL读取到java对象/存储在mysql数据库中

无法将JSON从URL读取到java对象/存储在mysql数据库中,java,mysql,json,Java,Mysql,Json,我是一名刚刚完成6个月编码速成课程的新手。我正在开发一个JavaWebApp来展示我的技能,我的项目想法涉及从API检索JSON数据,这是我们在课堂上没有学到的。我制作了POJO来匹配JSON,我试图将JSON解析为java对象以存储在数据库中,但是当我运行应用程序时,我的数据库表从来没有填充过数据。我怀疑问题出在我转换JSON的方法上,但是非常感谢您的反馈。这是我所有的代码,我认为是相关的,对不起,如果它的TMI。我也很抱歉,如果我的代码是丑陋的,我是一个初学者。。。谢谢 API返回的JSON

我是一名刚刚完成6个月编码速成课程的新手。我正在开发一个JavaWebApp来展示我的技能,我的项目想法涉及从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
}
控制器

@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扩展了Crudepository{
}

也许我的方法有点幼稚,但是。。。如果您想将JSON作为字符串存储在数据库中,那么我将使用对象映射器:

new ObjectMapper().writeValueAsString(myObject);
对于读取JSON并将其解析为类,我将执行以下操作:

new ObjectMapper().readValue(JSON_STRING_HERE, "utf-8"), MyPOJO.class);
另外,如果您已经在使用Spring,那么您的控制器可能看起来像这样(例如,对于POST)


那么,客户端发送到你的应用程序和你的控制器的JSON解析已经由Spring处理了

你在控制台或堆栈跟踪中得到任何异常吗?你能提供一个数据库吗schema@RajithPemabandu我没有收到该代码的任何异常,但数据库中没有存储任何内容,只有空表。@wontonimo抱歉,我不明白你的问题(我是新手)。这只是我用MAMP生成的一个mysql数据库。
new ObjectMapper().writeValueAsString(myObject);
new ObjectMapper().readValue(JSON_STRING_HERE, "utf-8"), MyPOJO.class);
@RequestMapping(value = "/", method = RequestMethod.POST)
public MyPojo myController(@RequestBody MyPojo myBody) {
    myRepository.save(myBody);
}