从浏览器查询字符串创建(json-)数组

时间:2013-07-25 15:31:30

标签: python regex json query-string

从geolocation 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访问值。任何人都可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

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

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))

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