【问题标题】:JSON parse error on object property value对象属性值的 JSON 解析错误
【发布时间】:2018-02-04 23:25:39
【问题描述】:

我是 JSON 的新手,我认为 JSON.parse 获得了您想要找到的键的值,或者至少是我从它的定义中理解的值。但是我遇到了一个我无法理解的错误。

未处理的 Promise 拒绝:JSON 输入意外结束;区域:角度;任务:Promise.then;值:SyntaxError:JSON 输入意外结束

我的最终结果是使用atob 为每个结果转换键的值。我的代码如下

    loadUserInfo() {
        this.getUsers()
            .then((result) => {
                for (const dx of result){
                    const signs = dx.signature;
                    console.log(JSON.parse(signs));
                }
                   // const conv = atob(decode64);
                   // const myImg = `data:image/jpeg;base64,${conv}`;
                    //console.log(myImg);

    };
}

如果我不使用 JSON.parse,我的代码会返回正确转换的 atob,但使用对象表示法

【问题讨论】:

  • 发布结果变量的样本。
  • 你不能解析已经是对象的东西。你的意思是字符串化?
  • @Pavlo 我不知道 stringfy 或 parse。
  • @Nofel 如果你不知道parse,你为什么要使用它?
  • @jeff 哪个阶段的结果? result?

标签: javascript json angular


【解决方案1】:

JSON 是对象表示法,用于序列化数据。

JSON.parse 是解析有效 JSON 字符串并输出 JavaScript 对象的函数。有一个相反的函数 - JSON.stringify - 将 JavaScript 对象序列化为有效的 JSON 字符串。

【讨论】:

  • 这行得通!但是如何将该字符串转换为 atob 并使其显示图像?我做错了什么?
  • @Nofel atob 将某些内容编码为 base64。 btoa 解码 base64 字符串。您确定需要使用哪一个(如果有的话)?
  • 图像是一个 blob,它是使用btoa 转换的,所以我假设如果我需要显示图像回来它是atob?我是 JS 新手
  • @Nofel 不,如果你的数据是 base64 格式,只需将其传递给 data:image/jpeg;base64,{{data}}
  • 我得到了following 输出,我认为它无效?
【解决方案2】:
loadUserInfo() {
    this.getUsers()
        .then((result) => {
            for (const dx of result){
                const signs = dx.signature;
                console.log(signs); //Show this
                console.log(JSON.parse(signs));
            }
        }).catch((reason) => { 
            console.log(reason); //show this too
        });
    }
}

如果你给了这些东西,那么我们就能看到哪里出了问题

【讨论】:

  • SyntaxError: Unexpected end of JSON input at JSON.parse (<anonymous>)
猜你喜欢
  • 2023-03-03
  • 2017-01-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多