【问题标题】:Backslash when i stringify my object of object of array of object in Javascript当我在Javascript中对对象数组的对象对象进行字符串化时,反斜杠
【发布时间】:2020-02-03 01:14:43
【问题描述】:

我在 React-native 上开发一个应用程序,我想将所有用户数据保存在服务器上并请求 API。 但是当我尝试时,我的请求是错误的。

{"body": "{
    \"residenceId\": \"5d88dfb55feb4c06a5cfb756\",
    \"date\": \"2019-10-04T16:37:10.048Z\",
    \"espaces\": [
        {
            \"espaceId\": \"5d88dfb55feb4c06a5cfb761\",
            \"photosPath\": [
                \"content://com.immodt.provider/root/storage/emulated/0/Pictures/image-2c97392b-fd7c-4480-9d9e-d055c74cab7e.jpg\"
            ],
            \"comment\": \"Un sol\"
        },
        {
            \"espaceId\": \"5d88dfb55feb4c06a5cfb760\",
            \"photosPath\": [
                \"content://com.immodt.provider/root/storage/emulated/0/Pictures/image-0518bb83-7d66-43f5-a0df-4e803bca50da.jpg\",
                \"content://com.immodt.provider/root/storage/emulated/0/Pictures/image-d416bd03-051f-43f0-8d09-478ad616562a.jpg\"
            ],
            \"comment\": \"Un plafond\"
        }
    ]
}"}

我不知道为什么。

我尝试过使用 Stringify,我尝试过不使用 stringify,每当我尝试我的“espaces”数组时遇到 stringify 的错误。 我认为我的数组是错误的核心。 我已经看到很多这样的错误主题,但没有人找到我的解决方案。

我的方法使用所有数据生成我的对象(并更改架构)

_saveAllData() {
        const token = this.props.token;
        let data = this.props.picturesAndComment;
        let date = new Date();

        let newData = {
            residenceId: this.props.residence._id,
            date: date,
            espaces: []
        };

        for (let i = 0, c = data.length; i < c; i++) {
            newData.espaces[i] = {
                espaceId: data[i].idSubspace,
                photosPath: [],
                comment: data[i].comment
            };

            for (let j = 0, d = data[i].pictures.length; j < d; j++) {
                newData.espaces[i].photosPath[j] = data[i].pictures[j].uri;
            }
        }
        console.log(newData);
        //this.setState({ isLoading: true });

        /*Promise.all([postVisit(token, newData), postPhoto(token, picturesDataForUpload)]).then((arrayOfResponses) => {
            console.warn(arrayOfResponses);
            this.setState({ isLoading: false });
        }).catch((error) => {
            console.warn('Error :', error);
            this.setState({ isLoading: false });
        });*/
    }
}

我用于发送数据的 API 方法

export function postVisit(token, data) {
    const url = "http://51.91.11.5:8080/visites/";
    const request = {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json',
            'Authorization': 'Bearer ' + token.accessToken
        },
        body: data
    };
    console.log(request);
    return fetch(url, request)
        .then((response) => response.json());
}

我希望一个没有反斜杠的普通 JSON...

【问题讨论】:

  • 如果没有转义,您将如何在用" 封装的字符串中表示"
  • 因为不是字符串,所以是数组...
  • 我建议首先创建一个minimal-reproducible-example - 删除所有无关信息,这将有助于人们更快地找到问题的根源
  • @MatthyDragneel 它是一个 字符串 你可以在你的输出中看到双引号 "body": "{ 从这里开始到最后以 }" 结束 - @ 的整个值987654329@是一串JSON数据。
  • 当我使用console.log 时,输出是一个字符串@VLAZ 我的console.log 像JSON 一样渲染我的每个对象,但它不是JSON,它是一个内部数组的对象。当我在请求中推送我的对象时,JSON 解析器向我发送 JSON 解析错误的错误。

标签: javascript json react-native stringify


【解决方案1】:

问题解决了。关心的是我客户的 API 而不是我的代码。谢谢你的回答。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-09-14
    • 1970-01-01
    • 1970-01-01
    • 2016-11-10
    • 2019-12-05
    • 2021-07-25
    • 2014-07-10
    • 1970-01-01
    相关资源
    最近更新 更多