循环遍历json中的对象数组,并将每个对象添加到bbdd中

我是Java Spring和所有Java生态系统的新手。我有一个JSON对象,其中包含对象数组。每个对象都必须映射并保存到oracle DB中。当然,每个对象在数据库中等于一行。

我必须映射的JSON:

{
  "success": true,
  "total": 3,
  "list": [
    {
      "id": "807990",
      "name": "Michael jordan",
      "tm_login": "1557989886",
      "tm_logout": "1558101623"
    },
    {
      "id": "807996",
      "name": "Lebron James",
      "tm_login": "1557989907",
      "tm_logout": "1558108803"
    },
    {
      "id": "808014",
      "name": "Kobe Bryant",
      "tm_login": "1558004258",
      "tm_logout": "1558108806"
    }
}

数据库模型


@Entity
@Table(name = "CALLS")
public class Calls {

    @Column(name = "ID")
    @Id
    private String id;

    @Column(name = "NAME")
    private String name;

    @Column(name = "TM_LOGIN")
    private String tmLogin;

    @Column(name = "TM_LOGOUT")
    private String tmLogout;

//GETTERS AND SETTERS

模型库

public interface CallsRepository extends JpaRepository<Calls,Long> {
}

我正在尝试的逻辑

 Calls calls = new Calls();
 JSONObject json = new JSONObject(json);

        JSONArray jsonArray = json.getJSONArray("list");

        for (int i = 0, size = jsonArray.length(); i < size; i++)
        {
            JSONObject objectInArray = jsonArray.getJSONObject(i);
            String[] elementNames = JSONObject.getNames(objectInArray);
            for (String elementName : elementNames)
            {
                String value = objectInArray.getString(elementName);
                calls.setName(value);
                callsRepository.save(calls);

            }
           return "all was good";
        }


如何在每次迭代中将每个对象插入数据库中的任何想法?

评论