JavaScript 推荐大家一个前端 query string 解析器

zhangky · August 07, 2015 · 2324 hits

Ruby on Rails 用 rack 风格的 query string 解析,sinatra 也是,网上很多前端 query string 解析,然而很多不支持 rack 的格式。 于是我写了一个解析器,很轻量,叫做 Query String Parser 项目在这里: https://github.com/cheunghy/query-parser

NPM 包地址:https://www.npmjs.com/package/query-string-parser

如果有人有兴趣可以做一个 gem 封装。

paramObject = {
  posts: [
    {
      'title': 'Post 1',
      'tags': ['node', 'script', 'javascript']
    },
    {
      'title': 'Post 2',
      'tags': ['node', 'why', 'not']
    }
  ],
  author: "Somebody",
  date: "Today"
}
queryString = objectToQuery(paramObject)
=>  "posts%5B%5D%5Btitle%5D=Post%201&posts%5B%5D%5Btags%5D%5B%5D=node&posts%5B%5D%5Btags%5D%5B%5D=script&posts%5B%5D%5Btags%5D%5B%5D=javascript&posts%5B%5D%5Btitle%5D=Post%202&posts%5B%5D%5Btags%5D%5B%5D=node&posts%5B%5D%5Btags%5D%5B%5D=why&posts%5B%5D%5Btags%5D%5B%5D=not&author=Somebody&date=Today"

parseQuery(queryString)
=> And the object comes back
No Reply at the moment.
You need to Sign in before reply, if you don't have an account, please Sign up first.