gpt4 book ai didi

java - 如何打印空字符串数组

转载 作者:行者123 更新时间:2023-12-02 11:41:13 24 4
gpt4 key购买 nike

我正在尝试从 .dat 文件读取数据,然后将它们写入 .json。问题是有些人的文件中没有电子邮件。所以,我想要类似的东西

"emails": []

 {
"personCode": "2342",
"firstName": "Jeff",
"lastName": "Spalding",
"address": {
"street": "123 Friendly Street",
"city": "Ottawa",
"state": "ON",
"zip": "K1A 0G9",
"country": "Canada"
},
"emails": []
},

但是,我的代码没有打印电子邮件[]

{
"personCode": "2342",
"firstName": "Jeff",
"lastName": "Spalding",
"address": {
"street": "123 Friendly Street",
"city": "Ottawa",
"state": "ON",
"zip": "K1A 0G9",
"country": "Canada"
}
}

我的代码:

String[] email = null;
String[] emailTokens = data[3].split(",");
for (int i = 0; i < emailTokens.length; i++) {
email = emailTokens;
}

Person person = new Person(personCode, firstName, lastName, address, email);
personList.add(person);

有什么想法吗?谢谢!

最佳答案

你的循环没有意义:

for (int i = 0; i < emailTokens.length; i++) {
email = emailTokens; // Here you assign the some emailTokens N times.
}

可以简化为:

String[] email = data[3].split(",");

关于 JSON 中丢失电子邮件的问题,请尝试传递空数组:

String[] email = data[3].split(",");
if (email == null) {
email = new String[0];
}

关于java - 如何打印空字符串数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48533230/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com