在Javascript中字符串化对象数组中对象的对象时使用反斜杠
我在React native上开发一个应用程序,我想将服务器上的所有用户数据保存到API请求中。 但当我尝试时,我的请求是错误的在Javascript中字符串化对象数组中对象的对象时使用反斜杠,javascript,json,react-native,stringify,Javascript,Json,React Native,Stringify,我在React native上开发一个应用程序,我想将服务器上的所有用户数据保存到API请求中。 但当我尝试时,我的请求是错误的 {"body": "{ \"residenceId\": \"5d88dfb55feb4c06a5cfb756\", \"date\": \"2019-10-04T16:37:10.048Z\", \"espaces\": [ { \"espaceId\": \"5d88dfb55feb4c06a5cfb
{"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的bug。
我认为我的阵列是bug的核心。
我看到过很多类似这样的bug主题,但没有人找到我的解决方案
我的方法使用所有数据生成我的对象并更改体系结构
_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 });
});*/
}
}
我期望一个正常的JSON,没有反斜杠…问题解决了。问题是我客户的API,而不是我的代码。感谢您的回答。如果不是转义,您如何在封装的字符串中表示a?因为它不是字符串,而是数组…我建议首先创建a-清除所有无关信息,这将有助于人们找到问题的根源faster@MatthyDragneel这是一个字符串,您可以在输出正文中看到双引号:{从这里开始,以}结尾-正文的整个值是一个JSON数据字符串。当i console.log时,输出是一个字符串@VLAZ My console.log将我的每个对象呈现为JSON,但它不是一个JSON,它是一个具有内部数组的对象。当我在请求中推送对象时,JSON解析器会向我发送一个JSON解析错误的错误。
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());
}