Frage

I'm working on manipulating data from a program, and I came across this data format, but I can't figure out how to parse it.

response="0",num=3,list=[
{type="url1",url="http://www.xxx1.com"},
{type="url2",url="http://www.xxx2.com"},
{type="url3",url="http://www.xxx3.com"}
],type="LIST", id=1

Does anyone have any suggestions?

Thanks!

War es hilfreich?

Lösung

I don't know what this format is, but it is very close to JSON.

All you need is to replace key= with "key": and wrap around extra braces to make it valid JSON, so then you can use any JSON library to parse it.

You can parse it using this Perl code:

use JSON::XS;

my $input = qq{
    response="0",num=3,list=[
    {type="url1",url="http://www.xxx1.com"},
    {type="url2",url="http://www.xxx2.com"},
    {type="url3",url="http://www.xxx3.com"}
    ],type="LIST", id=1
};
my $str = "{" . $input . "}";
$str =~ s/(\w+)=/"$1":/g; # replace key= with "key": (fragile!)
my $json = decode_json($str);
# at this point, $json is object containing all fields you need.
# ...

Andere Tipps

python:

import json
import re
str = """response="0",num=3,list=[
{type="url1",url="http://www.xxx1.com"},
{type="url2",url="http://www.xxx2.com"},
{type="url3",url="http://www.xxx3.com"}
],type="LIST", id=1"""
fn = lambda m: '"' + m.group(1) + '":'
json_str = "{"+re.sub(r'(\w+)=', fn, str)+"}"
print json_str
print "==========================="
dict_obj = json.loads(json_str)
print dict_obj
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top