Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/319.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
我应该将CSV json字符串强制转换为它的java成员吗_Java_Android_Json_Getter Setter - Fatal编程技术网

我应该将CSV json字符串强制转换为它的java成员吗

我应该将CSV json字符串强制转换为它的java成员吗,java,android,json,getter-setter,Java,Android,Json,Getter Setter,我有一个json对象,我想将其装箱到java类中,我不确定我应该将其中两个属性转换为什么,它们是逗号分隔的字符串。json对象如下所示 {"color":null,"display_name":null,"id":321,"option_codes":"MS01,RENA,TM00,DRLH,PF00,BT85,PBCW,RFPO,WT19,IBMB,IDPB,TR00,SU01,SC01,TP01,AU01,CH00,HP00,PA00,PS00,AD02,X020,X025,X001,X00

我有一个json对象,我想将其装箱到java类中,我不确定我应该将其中两个属性转换为什么,它们是逗号分隔的字符串。json对象如下所示

{"color":null,"display_name":null,"id":321,"option_codes":"MS01,RENA,TM00,DRLH,PF00,BT85,PBCW,RFPO,WT19,IBMB,IDPB,TR00,SU01,SC01,TP01,AU01,CH00,HP00,PA00,PS00,AD02,X020,X025,X001,X003,X007,X011,X013","user_id":123,"vehicle_id":1234567890,"vin":"5YJSA1CN5CFP01657","tokens":["x","x"],"state":"online"}
选项代码和标记是成员属性,我经常在这个java类中遇到错误

public class Vehicle{
private String color,displayName,vin,state;
private int id,user_id,vehicle_id;
private List<String> option_codes,tokens;
public String getColor() {
    return color;
}
public void setColor(String color) {
    this.color = color;
}
public String getDisplayName() {
    return displayName;
}
public void setDisplayName(String displayName) {
    this.displayName = displayName;
}
public String getVin() {
    return vin;
}
public void setVin(String vin) {
    this.vin = vin;
}
public String getState() {
    return state;
}
public void setState(String state) {
    this.state = state;
}
public int getId() {
    return id;
}
public void setId(int id) {
    this.id = id;
}
public int getUser_id() {
    return user_id;
}
public void setUser_id(int user_id) {
    this.user_id = user_id;
}
public int getVehicle_id() {
    return vehicle_id;
}
public void setVehicle_id(int vehicle_id) {
    this.vehicle_id = vehicle_id;
}
public List<String> getOption_codes() {
    return option_codes;
}
public void setOption_codes(List<String> option_codes) {
    this.option_codes = option_codes;
}
public List<String> getTokens() {
    return tokens;
}
public void setTokens(List<String> tokens) {
    this.tokens = tokens;
}
公共级车辆{
私有字符串颜色、显示名称、vin、状态;
私人int id、用户id、车辆id;
私有列表选项\u代码、令牌;
公共字符串getColor(){
返回颜色;
}
公共void setColor(字符串颜色){
这个颜色=颜色;
}
公共字符串getDisplayName(){
返回显示名;
}
public void setDisplayName(字符串displayName){
this.displayName=displayName;
}
公共字符串getVin(){
返回vin;
}
公共无效设置vin(字符串vin){
这个。vin=vin;
}
公共字符串getState(){
返回状态;
}
公共无效设置状态(字符串状态){
this.state=状态;
}
公共int getId(){
返回id;
}
公共无效集合id(内部id){
this.id=id;
}
public int getUser_id(){
返回用户id;
}
public void setUser_id(int user_id){
this.user\u id=user\u id;
}
public int getVehicle_id(){
归还车辆标识;
}
公共车辆id(内部车辆id){
this.vehicle\u id=车辆id;
}
公共列表getOption_代码(){
返回选项_代码;
}
公共无效设置选项代码(列出选项代码){
this.option_code=option_code;
}
公共列表getTokens(){
归还代币;
}
公共无效设置令牌(列出令牌){
this.tokens=代币;
}
}


是否有办法从csv字符串将选项代码强制转换为数组?

您需要在车辆类别中为列表分配空间:

private List<String> option_codes = new ArrayList<String>();
private List<String> tokens = new ArrayList<String>();

请查看。

您的setter可以根据字符串制作列表:

public void setOption_codes(String option_codes) {
  this.option_codes = Splitter.on(',').splitToList(option_codes);
}

您可以使用@JsonDeserialize和@JsonSerialize注释。实现将字符串转换为列表的适当类,反之亦然。因此,在您的类中,json中将包含列表和逗号分隔的字符串。

您可以在“,”上拆分它。但是直接转换到数组是不可能的,因为这不是我所想的arrayKinda,我只需要手动转换字符串??是的,我想你必须这样做
public void setOption_codes(String option_codes) {
  this.option_codes = Splitter.on(',').splitToList(option_codes);
}