【问题标题】:Create (json-) array from browser query string从浏览器查询字符串创建 (json-) 数组
【发布时间】:2013-07-25 15:31:29
【问题描述】:

从地理定位 api 浏览器查询中,我得到:

browser=opera&sensor=true&wifi=mac:B0-48-7A-99-BD-86|ss:-72|ssid:Baldur WLAN|age:4033|chan:6&wifi=mac:00-24-FE-A7-BA-94|ss:-83|ssid:wlan23-k!17|age:4033|chan:10&wifi=mac:90-F6-52-3F-60-64|ss:-95|ssid:Baldur WLAN|age:4033|chan:13&device=mcc:262|mnc:7|rt:3&cell=id:15479311|lac:21905|mcc:262|mnc:7|ss:-107|ta:0&location=lat:52.398529|lng:13.107570

我想访问所有本地结构化的单个值。我的方法是更深入地创建一个 json 数组,而不是先将其拆分为“&”,然后将其拆分为“=”以获取查询中所有值的数组。另一种方法是使用正则表达式 (\w+)=(.*) 在用“&”分割后以相同的深度结束,但我需要更多的细节可以作为数据类型访问。

生成的数组应如下所示:

{
    "browser": ["opera"],
    ...
    "location":  [{
                     "lat": 52.398529,
                     "lng": 13.107570
                 }],
    ...
    "wifi": [{    
                 "mac": "00-24-FE-A7-BA-94",
                 "ss": -83,
                 ...
            },
            {    
                 "mac": "00-24-FE-A7-BA-94",
                 "ss": -83,
                 ...
            }]

或者类似的东西,我可以用额外的 json 库解析以使用 python 访问值。有人可以帮忙吗?

【问题讨论】:

    标签: python regex json query-string


    【解决方案1】:

    这里是从字典传递的解决方案

    import re
    import json
    
    transform a string to a dictionary, sepfield is the field separator, 
    def str_to_dict(s, sepfield, sepkv, infields=None):
        """ transform a string to a dictionary
           s: the string to transform
           sepfield: the string with the field separator char
           sepkv: the string with the key value separator
           infields: a function to be applied to the values
    
           if infields is defined a list of elements with common keys returned
           for each key, otherwise the value is associated to the key as it is"""
    
        pattern = "([^%s%s]*?)%s([^%s]*)"  % (sepkv, sepfield, sepkv, sepfield)
        matches = re.findall(pattern, s)
        if infields is None:
            return dict(matches)
        else:
            r=dict()
            for k,v in matches:
                parsedval=infields(v)
                if k not in r:
                    r[k] = []
                r[k].append(parsedval)
            return r
    
    def second_level_parsing(x):
        return x if x.find("|")==-1 else str_to_dict(x, "|",":")
    
    json.dumps(str_to_dict(s, "&", "=", second_level_parsing))
    

    您可以轻松扩展多个级别。请注意,是否定义了 infields 函数的不同行为是匹配您要求的输出。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-28
      • 2013-10-25
      • 2016-05-17
      相关资源
      最近更新 更多