我正在努力操作程序中的数据,我遇到了这种数据格式,但我无法弄清楚如何解析它。
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
有人有任何建议吗?
谢谢!
答案 0 :(得分:0)
我不知道这种格式是什么,但它非常接近JSON。
您只需要将key=
替换为"key":
并使用额外的大括号使其成为有效的JSON,然后您可以使用任何JSON库来解析它。
您可以使用此Perl代码解析它:
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.
# ...
答案 1 :(得分:0)
蟒:
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