英文:
replace null to empty string in Java
问题
我有以下字符串输出:
["Kolkata","data can be, null",null,"05/31/2020",null]
但我想要的输出格式如下,在Java中:
["Kolkata","data can be, null","","05/31/2020",""]
请帮忙。
我正在将对象转换为JSON数据。请查看以下代码
List<String> test = new ArrayList<>();
List<Object[]> data = query.list();
for (int i = 0; i < data.size(); i++) {
Object[] row = (Object[]) data.get(i);
String jsonString = gson.toJson(row);
test.add(jsonString);
}
我想在jsonString变量上应用这个,使用Java 7,而不是使用Java 8。
英文:
I have below string output :
["Kolkata","data can be, null",null,"05/31/2020",null]
but I want to have the output like below format in Java
["Kolkata","data can be, null","","05/31/2020",""]
please help me .
I am converting object to json data . Please see the below codes
List<String> test = new ArrayList<>();
List<Object[]> data =query.list();
for (int i = 0; i < data.size(); i++) {
Object[] row = (Object[]) data.get(i);
String jsonString = gson.toJson(row);
test.add(jsonString);
}
I want to apply this on jsonString variable using java 7 as not using java 8
答案1
得分: 1
Sure, here's the translated content:
如果你有一个列表,例如像这样的列表:
List<String> list = Arrays.asList("Kolkata", "data can be, null", null, "05/31/2020", null);
list.replaceAll(t -> Objects.isNull(t) ? "''" : t);
System.out.println(list);
这里的输出将会是:
[Kolkata, data can be, null, '', 05/31/2020, '']
英文:
If you have list for example list of like this
List<String> list = Arrays.asList("Kolkata","data can be, null",null,"05/31/2020",null);
list.replaceAll(t -> Objects.isNull(t) ? "''" : t);
System.out.println(list);
Here oputput will be:
[Kolkata, data can be, null, '', 05/31/2020, '']
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论