qs
qs copied to clipboard
qs.stringify(json) and qs.parse(json) Results are inconsistent with expectations
When my json is like this
[
{
"fansUid": "",
"masterShopId": 0,
"items": [
{
"itemId": "",
"skus": [
{
"price": 0,
"skuId": 0,
"promotionToolCode": ""
}
]
}
],
"scene": ""
}
]
i use qs.stringify(json) and qs.parse(json) . but result is
[
{
"fansUid": "",
"masterShopId": "0",
"items": [
{
"itemId": "",
"skus": [
{
"[price]": "0",
"[skuId]": "0",
"[promotionToolCode]": ""
}
]
}
],
"scene": ""
}
]
It was obvious that this was a mistake. How should I solve it
I tried to change multiple versions as well, but the latest version still did not fix the problem.
I agree this is a bug.
In general, I'd pass options to both stringify and parse, but the defaults should indeed round-trip.
I don't know if this would be considered a bug. There simply isn't a way to know what the original type a property was after it has been stringify'd. It's not guaranteed that all users will want numbers converted back to an integer type.
You could create some type of mapping to run your parsed response through which ensures the data types are converted to your liking.
hmm, that's a good counterpoint.
The property names have brackets around them too: [price]
vs price
.