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

zhangky · 2015年08月07日 · 2321 次阅读

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
暂无回复。
需要 登录 后方可回复, 如果你还没有账号请 注册新账号