将JSON字符串[]映射到表问题

时间:2018-09-10 05:37:39

标签: java json string

我正在提取网站的API,而我的Jason返回的内容如下:

{  
   "stats":{  
      "corners":[  
         "11",
         "1"
      ],
      "yellowcards":[  
         "1",
         "3"
      ],
      "redcards":[  
         "0",
         "0"
      ],
      "penalties":[  
         "0",
         "0"
      ],
      "substitutions":[  
         "0",
         "0"
      ],
      "attacks":[  
         "51",
         "51"
      ],
      "dangerous_attacks":[  
         "32",
         "31"
      ],
      "on_target":[  
         "5",
         "7"
      ],
      "off_target":[  
         "13",
         "5"
      ],
      "possession_rt":[  
         "55",
         "45"
      ]
   }
}

我试图将其转换为Java类,如下所示:

打包com.events.bettingevents.results;

import org.hibernate.annotations.GenericGenerator;

import javax.persistence.*;
import java.io.Serializable;
import java.util.Arrays;
import java.util.List;

@Entity
@Table(name="resbet_stats")
public class Stats implements Serializable
{
    private String[] off_target;
    private String[] possession_rt;
    private String[] redcards;
    private String[] dangerous_attacks;
    private String[] corners;
    private String[] substitutions;
    private String[] on_target;
    private String[] yellowcards;
    private String[] attacks;
    private String[] penalties;
    private String[] goals;
    private Long statsId;
    private Results results;


    @OneToOne(mappedBy = "stats")
    public Results getResults() {
        return results;
    }

    public void setResults(Results results) {
        this.results = results;
    }

    @Id
    @GeneratedValue(strategy= GenerationType.AUTO,generator="native")
    @GenericGenerator(name = "native",strategy = "native" )
    @Column(name="stats_id")
    public Long getStatsId() {
        return statsId;
    }

    public void setStatsId(Long statsId) {
        this.statsId = statsId;
    }

    @Column(name="goals")
    public String[] getGoals() {
        return goals;

    }

    public void setGoals(String[] goals) {
        this.goals = goals;

    }

    @Column(name="off_target")
    public String[] getOff_target ()
    {
       return off_target;

    }

    public void setOff_target (String[] off_target)
    {
        this.off_target = off_target;

    }

    @Column(name="possession_rt")
    public String[] getPossession_rt ()
    {
        return possession_rt;

    }

    public void setPossession_rt (String[] possession_rt)
    {
        this.possession_rt = possession_rt;

    }

    @Column(name="redcards")
    public String[] getRedcards ()
    {
        return redcards;

    }

    public void setRedcards (String[] redcards)
    {
        this.redcards = redcards;

    }

    @Column(name="dangerous_attacks")
    public String[] getDangerous_attacks ()
    {
        return dangerous_attacks;

    }

    public void setDangerous_attacks (String[] dangerous_attacks)
    {
        this.dangerous_attacks = dangerous_attacks;

    }

    @Column(name="corners")
    public String[] getCorners ()
    {
        return corners;

    }

    public void setCorners (String[] corners)
    {
        this.corners = corners;

    }

    @Column(name="substitutions")
    public String[] getSubstitutions ()
    {
        return substitutions;

    }

    public void setSubstitutions (String[] substitutions)
    {
        this.substitutions = substitutions;

    }

    @Column(name="on_target")
    public String[] getOn_target ()
    {
        return on_target;

    }

    public void setOn_target (String[] on_target)
    {
        this.on_target = on_target;

    }

    @Column(name="yellowcards")
    public String[] getYellowcards ()
    {
        return yellowcards;

    }

    public void setYellowcards (String[] yellowcards)
    {
        this.yellowcards = yellowcards;

    }


    @Column(name="attacks")
    public String[] getAttacks (){
        return attacks;
    }

    public void setAttacks (String[] attacks){
        this.attacks = attacks;

    }

    @Column(name="penalties")
    public String[] getPenalties ()
    {
        return penalties;

    }


    public void setPenalties (String[] penalties)
    {
        this.penalties = penalties;

    }


}



Can someone suggest what datatype for table columns should I use? Is it blob? I created all my columns as `varchar`(10).

我遇到类似的错误:

第1行的列'attacks'的字符串值不正确:'\ xAC \ xED \ x00 \ x05ur ...' : : : 感谢您的提前帮助。

1 个答案:

答案 0 :(得分:1)

使用ElementCollection存储字符串集合。像这样:

@ElementCollection
@CollectionTable(name="my_collection_table", joinColumns=@JoinColumn(name="stats_id"))
public Collection<String> getGoals(){
    return goals;
}

您需要为每个集合创建一个表,该表的列值和外键引用拥有的实体。