js中JSON与QS的异同点
相同点
- 二者可以进行序列化与反序列化;
- 二者都有相同的方法:序列化:stringify() 反序列化:parse()
不同点
- JSON.stringify()方法是将对象转化为JSON格式
- qs.stringify()方法是将对象转化为url格式
- JSON.parse()方法是将JSON数据转化为对象
- qs.parse()方法是将字符串转化为对象
实践出真知:
let par = {
start: '2018-12-1T02:00:00Z',
stop: '2018-12-2T02:00:00Z',
step: '60',
epoch: '2018-12-1T02:00:00Z',
SemimajorAxis: '6711000',
element: {
Eccentricity: '0.01',
Inclination: '43',
ArgumentOfPerigee: '10',
RAAN: '20',
MeanAnomaly: '30',
},
propagatorType: '0',
orbitDataType: '0'
};
const jsonS = JSON.stringify(par);
console.log(jsonS);
// {"start":"2018-12-1T02:00:00Z","stop":"2018-12-2T02:00:00Z","step":"60","epoch":"2018-12-1T02:00:00Z","element":{"SemimajorAxis":"6711000","Eccentricity":"0.01","Inclination":"43","ArgumentOfPerigee":"10","RAAN":"20","MeanAnomaly":"30"},"propagatorType":"0","orbitDataType":"0"}
const jsonP = JSON.parse(jsonS);
console.log(jsonP);
// {start: "2018-12-1T02:00:00Z", stop: "2018-12-2T02:00:00Z", step: "60", epoch: "2018-12-1T02:00:00Z", element: {…}, …}
const qsS = qs.stringify(par);
console.log(qsS);
// start=2018-12-1T02%3A00%3A00Z&stop=2018-12-2T02%3A00%3A00Z&step=60&epoch=2018-12-1T02%3A00%3A00Z&element%5BSemimajorAxis%5D=6711000&element%5BEccentricity%5D=0.01&element%5BInclination%5D=43&element%5BArgumentOfPerigee%5D=10&element%5BRAAN%5D=20&element%5BMeanAnomaly%5D=30&propagatorType=0&orbitDataType=0
const qsP = qs.parse(qsS);
console.log(qsP)
// {start: "2018-12-1T02:00:00Z", stop: "2018-12-2T02:00:00Z", step: "60", epoch: "2018-12-1T02:00:00Z", element: {…}, …}