【问题标题】:What is this object notation format?这个对象符号格式是什么?
【发布时间】:2013-06-09 08:03:04
【问题描述】:

我正在处理程序中的数据,我遇到了这种数据格式,但我不知道如何解析它。

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

有人有什么建议吗?

谢谢!

【问题讨论】:

    标签: object format notation


    【解决方案1】:

    我不知道这种格式是什么,但它非常接近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.
    # ...
    

    【讨论】:

      【解决方案2】:

      蟒蛇:

      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
      

      【讨论】:

        猜你喜欢
        • 2010-11-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-01-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多