0

From the geolocation api browser query, I get this:

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

I would like to access all the single values local structured. My approach is to create a json array more in depth, than split it up by "&" first and "=" afterwards to get an array of all values in the query. Another approach is to use regex (\w+)=(.*) after splitting by "&" ends in the same depth but I need there more details accessible as datatype.

The resulting array should look like:

{
    "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,
                 ...
            }]

Or something similar that I can parse with an additional json library to access the values using python. Can anyone help with this?

4

1 回答 1

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

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

于 2013-07-25T16:38:36.133 回答