简体   繁体   English

使用python将以下查询字符串解析为json

[英]Parse the following query string into json using python

I have a query string with the following format: 我有以下格式的查询字符串:

 cmd=get-records&limit=100&offset=0&search[0][field]=number&search[0][type]=text&search[0][operator]=contains&search[0][value]=Mike+Jones&search[1][field]=name&search[1][type]=text&search[1][operator]=contains&search[1][value]=Mike+Jones&search[2][field]=role&search[2][type]=text&search[6]&searchLogic=OR

How can I convert this to structured json like the following (or similar): 如何将其转换为结构化json,如下所示(或类似内容):

{
  cmd: "...",
  limit: "...",
  offset: "...",
  search: {
    0: {
      number: "..."
      name: "...",
      ...
    }
    1: {
      ...
    }
    ...
  }, 
  ...
}

I have tried to use urlparse.parse_qs but it translates the query string to the following: 我尝试使用urlparse.parse_qs但是它将查询字符串转换为以下内容:

{
  "cmd": ["..."],
  "limit": ["..."],
  "offset": ["..."],
  "search[0][number]": ["..."],
  "search[0][name]": ["..."],
  "search[1][number]": ["..."].
  ...
}

The problem with this is the search fields. 问题在于搜索字段。 I want this to be correctly structured. 我希望这个结构正确。 The technologies I am using are as following: 我使用的技术如下:

Frontend: 前端:

w2ui table that requests data from the backend. w2ui表从后端请求数据。 Also, as shown in this example, when doing a search it sends a request to the backend to do the search. 同样,如本例所示,在进行搜索时,它会向后端发送一个请求以进行搜索。

Backend: 后端:

Django. Django的。 The post request from w2ui is handled by a view which takes in the query string and acts accordingly. 来自w2ui的发布请求由一个视图处理,该视图接受查询字符串并采取相应的行动。

Take a look at the querystring-parser package, which does exactly what you need: 看一看querystring-parser软件包,它确实满足您的需求:

import pprint

from querystring_parser import parser as qsparser

# I had to modify the query string you provided by adding a value
# search[6]; it didn't have one before, which caused an exception
query_string = (
    'cmd=get-records&limit=100&offset=0&search[0][field]=number&'
    'search[0][type]=text&search[0][operator]=contains&'
    'search[0][value]=Mike+Jones&search[1][field]=name&search[1][type]=text&'
    'search[1][operator]=contains&search[1][value]=Mike+Jones&'
    'search[2][field]=role&search[2][type]=text&search[6]=SEARCH6&'
    'searchLogic=OR'
    )  # NOTE: I had

query_string_as_dict = qsparser.parse(query_string)

pprint.pprint(query_string_as_dict)

The result is: 结果是:

{u'cmd': u'get-records',
 u'limit': 100,
 u'offset': 0,
 u'search': {0: {u'field': u'number',
                 u'operator': u'contains',
                 u'type': u'text',
                 u'value': u'Mike Jones'},
             1: {u'field': u'name',
                 u'operator': u'contains',
                 u'type': u'text',
                 u'value': u'Mike Jones'},
             2: {u'field': u'role', u'type': u'text'},
             6: u'SEARCH6'},
 u'searchLogic': u'OR'}

If you want it as JSON: 如果您希望将其作为JSON:

import json

json_string = json.dumps(query_string_as_dict)

Perhaps take a look at this library: querystring-parser 也许看看这个库: querystring-parser

It takes section[1]['words'][2]=a&section[0]['words'][2]=a&section[0]['words'][2]=b and converts it to {u'section': {0: {u'words': {2: [u'a', u'b']}}, 1: {u'words': {2: u'a'}}}} which looks like what you're after. 它需要section[1]['words'][2]=a&section[0]['words'][2]=a&section[0]['words'][2]=b并将其转换为{u'section': {0: {u'words': {2: [u'a', u'b']}}, 1: {u'words': {2: u'a'}}}} ,看起来像你在追求什么。

Their docs for using it within Django: 他们在Django中使用它的文档:

from querystring_parser import parser
post_dict = parser.parse(request.GET.urlencode())

如果您使用的是Django,则可以在视图内部按request.GET组织此类信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM