这是我要序列化的对象:
{"cars": [
{"make":"volkswagen","model":"golf"},
{"make":"volkswagen","model":"polo"},
{"make":"audi","model":"a3"},
{"make":"bmw","model":"1-series"},
{"make":"renault","model":"espace"}
]}
之前,我使用jQuery $.param()
函数和qs
库在node.js上解码它。一切都很好,我自己也不需要解析任何东西。
现在,我决定使用新的Angular 1.4服务$httpParamSerializer
来摆脱jQuery。解码后,而不是数组中的objects
,我得到strings
:
'{"make":"volkswagen","model":"golf"}'
如何正确解码使用$httpParamSerializer
服务创建的查询字符串,而不在数组中的每个对象上调用JSON.parse
?
这是$.param
querystring:
cars%5B0%5D%5Bmake%5D=volkswagen&cars%5B0%5D%5Bmodel%5D=golf&cars%5B1%5D%5Bmake%5D=volkswagen&cars%5B1%5D%5Bmodel%5D=polo&cars%5B2%5D%5Bmake%5D=audi&cars%5B2%5D%5Bmodel%5D=a3&cars%5B3%5D%5Bmake%5D=bmw&cars%5B3%5D%5Bmodel%5D=1-series&cars%5B4%5D%5Bmake%5D=renault&cars%5B4%5D%5Bmodel%5D=espace&demo%5Bsome%5D=bar
这里是$httpParamSerializer
querystring:
cars=%7B%22make%22:%22volkswagen%22,%22model%22:%22golf%22%7D&cars=%7B%22make%22:%22volkswagen%22,%22model%22:%22polo%22%7D&cars=%7B%22make%22:%22audi%22,%22model%22:%22a3%22%7D&cars=%7B%22make%22:%22bmw%22,%22model%22:%221-series%22%7D&cars=%7B%22make%22:%22renault%22,%22model%22:%22espace%22%7D&demo=%7B%22some%22:%22bar%22%7D`
答案 0 :(得分:0)
还有$ HttpParamSerializerJQLikeProvider,它像jquery param一样序列化。来自文档,
* Alternative {@link $http `$http`} params serializer that follows
* jQuery's [`param()`](http://api.jquery.com/jquery.param/) method logic.
* The serializer will also sort the params alphabetically.