在Java中将null替换为空字符串。

huangapple go评论68阅读模式
英文:

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&lt;String&gt; list = Arrays.asList(&quot;Kolkata&quot;,&quot;data can be, null&quot;,null,&quot;05/31/2020&quot;,null);
	list.replaceAll(t -&gt; Objects.isNull(t) ? &quot;&#39;&#39;&quot; : t);
	System.out.println(list);

Here oputput will be:

[Kolkata, data can be, null, &#39;&#39;, 05/31/2020, &#39;&#39;]

huangapple
  • 本文由 发表于 2020年4月8日 01:54:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/61086334.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定