如何在JSON文件中删除空字符串?

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

How to remove empty string in JSON file?

问题

Nuxt 在接收到空字符串的 JSON 时会返回一个错误:

示例错误:

input must be a string (received string: "")

在 Nuxt 中的循环示例:

<Grid class="sponsors">
  <Img v-for="prop in home" :src="prop.sponsors" :width="300" />
</Grid>

JSON 内容:

[
  {
    "id": "1",
    "title": "电影",
    "desc": "电影描述",
    "sponsors": "sponsors/culture-fond_ftl1iu_nhpivj.png",
    "films": "电影1"
  },
  {
    "id": "2",
    "title": "",
    "desc": "",
    "sponsors": "sponsors/muzey-tavrida-bw_wedmvw_qf4ixd.png",
    "films": "电影2"
  },
  {
    "id": "3",
    "title": "",
    "desc": "",
    "sponsors": "sponsors/insightstudio-logo-white_rc0vme.png",
    "films": "电影3"
  },
  {
    "id": "4",
    "title": "",
    "desc": "",
    "sponsors": "",
    "films": "电影4"
  },
  {
    "id": "5",
    "title": "",
    "desc": "",
    "sponsors": "",
    "films": "电影5"
  },
  {
    "id": "",
    "title": "",
    "desc": "",
    "sponsors": "",
    "films": "电影6"
  }
]

如何从 JSON 中删除空字符串?或者如何忽略这个错误?

我使用的是 Nuxt 3.4.1。

英文:

Nuxt returns an error when it receives empty strings from JSON:

Example error:

input must be a string (received string: &quot;&quot;)

An example of my loop in Nuxt:

&lt;Grid class=&quot;sponsors&quot;&gt;
  &lt;Img v-for=&quot;prop in home&quot; :src=&quot;prop.sponsors&quot; :width=&quot;300&quot; /&gt;
&lt;/Grid&gt;

JSON content:

[
{
&quot;id&quot;: &quot;1&quot;,
&quot;title&quot;: &quot;Film&quot;,
&quot;desc&quot;: &quot;Film desc&quot;,
&quot;sponsors&quot;: &quot;sponsors/culture-fond_ftl1iu_nhpivj.png&quot;,
&quot;films&quot;: &quot;film 1&quot;
},
{
&quot;id&quot;: &quot;2&quot;,
&quot;title&quot;: &quot;&quot;,
&quot;desc&quot;: &quot;&quot;,
&quot;sponsors&quot;: &quot;sponsors/muzey-tavrida-bw_wedmvw_qf4ixd.png&quot;,
&quot;films&quot;: &quot;film 2&quot;
},
{
&quot;id&quot;: &quot;3&quot;,
&quot;title&quot;: &quot;&quot;,
&quot;desc&quot;: &quot;&quot;,
&quot;sponsors&quot;: &quot;sponsors/insightstudio-logo-white_rc0vme.png&quot;,
&quot;films&quot;: &quot;film 3&quot;
},
{
&quot;id&quot;: &quot;4&quot;,
&quot;title&quot;: &quot;&quot;,
&quot;desc&quot;: &quot;&quot;,
&quot;sponsors&quot;: &quot;&quot;,
&quot;films&quot;: &quot;film 4&quot;
},
{
&quot;id&quot;: &quot;5&quot;,
&quot;title&quot;: &quot;&quot;,
&quot;desc&quot;: &quot;&quot;,
&quot;sponsors&quot;: &quot;&quot;,
&quot;films&quot;: &quot;film 5&quot;
},
{
&quot;id&quot;: &quot;&quot;,
&quot;title&quot;: &quot;&quot;,
&quot;desc&quot;: &quot;&quot;,
&quot;sponsors&quot;: &quot;&quot;,
&quot;films&quot;: &quot;film 6&quot;
}
]

How do I remove empty string from JSON? Or how to ignore this error?

I use Nuxt 3.4.1

答案1

得分: 2

多种方法来解决这个问题。

您可以在JavaScript中使用filter()(或其他框架中的类似方法)

const filteredData = jsonData.filter(obj => Object.values(obj).some(val => val !== ""));

另一种解决方法是在Nuxt中渲染IMG组件之前检查空字符串。

<Grid class="sponsors">
  <Img v-for="prop in home" :src="prop.sponsors" :width="300" v-if="prop.sponsors !== ''" />
</Grid>
英文:

Multiple ways to solve this problem.

You can use filter() in javascript (or something similar in other frameworks)

const filteredData = jsonData.filter(obj =&gt; Object.values(obj).some(val =&gt; val !== &quot;&quot;));

Another way to solve it would be to check for empty strings before rending the IMG component in Nuxt.

&lt;Grid class=&quot;sponsors&quot;&gt;
  &lt;Img v-for=&quot;prop in home&quot; :src=&quot;prop.sponsors&quot; :width=&quot;300&quot; v-if=&quot;prop.sponsors !== &#39;&#39;&quot; /&gt;
&lt;/Grid&gt;

huangapple
  • 本文由 发表于 2023年5月29日 15:44:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/76355502.html
匿名

发表评论

匿名网友

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

确定