Ruby在括号内解析JSON

时间:2014-01-28 05:10:02

标签: ruby json

我有一些生成错误的JSON,例如:

p_response({
    "glossary": {
        "title": "example glossary",
        "GlossDiv": {
            "title": "S",
            "GlossList": {
                "GlossEntry": {
                    "ID": "SGML",
                    "SortAs": "SGML",
                    "GlossTerm": "Standard Generalized Markup Language",
                    "Acronym": "SGML",
                    "Abbrev": "(ISO 8879:1986)",
                    "GlossDef": {
                        "para": "A meta-markup language, used to create markup languages such as DocBook.",
                        "GlossSeeAlso": ["GML", "XML"]
                    },
                    "GlossSee": "markup"
                }
            }
        }
    }
})

如何在p_response括号中解析Json?我不能使用gsub,因为JSON主体可能在其中有括号。

3 个答案:

答案 0 :(得分:1)

结果是JSONP。

只需删除潜在客户p_response(并追踪)。然后你可以把它解析为JSON:

jsonp = <<JSONP
p_response({
    "glossary": {
        "title": "example glossary",
        "GlossDiv": {
            "title": "S",
            "GlossList": {
                "GlossEntry": {
                    "ID": "SGML",
                    "SortAs": "SGML",
                    "GlossTerm": "Standard Generalized Markup Language",
                    "Acronym": "SGML",
                    "Abbrev": "(ISO 8879:1986)",
                    "GlossDef": {
                        "para": "A meta-markup language, used to create markup languages such as DocBook.",
                        "GlossSeeAlso": ["GML", "XML"]
                    },
                    "GlossSee": "markup"
                }
            }
        }
    }
})
JSONP

require 'json'
json = jsonp.gsub(/\Ap_response\(|\)\Z/, '')
# OR = jsonp.gsub(/\A\w+\(|\)\Z/, '')
puts JSON.parse(json)

# \A - Matches beginning of string.
# \Z - Matches end of string. If string ends with a newline, it matches just before newline

输出:

{"glossary"=>{"title"=>"example glossary", "GlossDiv"=>{"title"=>"S", "GlossList"=>{"GlossEntry"=>{"ID"=>"SGML", "SortAs"=>"SGML", "GlossTerm"=>"Standard Generalized Markup Language", "Acronym"=>"SGML", "Abbrev"=>"(ISO 8879:1986)", "GlossDef"=>{"para"=>"A meta-markup language, used to create markup languages such as DocBook.", "GlossSeeAlso"=>["GML", "XML"]}, "GlossSee"=>"markup"}}}}}

答案 1 :(得分:0)

json_str = '{
    "glossary": {
        "title": "example glossary",
        "GlossDiv": {
            "title": "S",
            "GlossList": {
                "GlossEntry": {
                    "ID": "SGML",
                    "SortAs": "SGML",
                    "GlossTerm": "Standard Generalized Markup Language",
                    "Acronym": "SGML",
                    "Abbrev": "(ISO 8879:1986)",
                    "GlossDef": {
                        "para": "A meta-markup language, used to create markup languages such as DocBook.",
                        "GlossSeeAlso": ["GML", "XML"]
                    },
                    "GlossSee": "markup"
                }
            }
        }
    }
}'

require 'json'
j = JSON.parse json_str

答案 2 :(得分:0)

您的回复是JSONP。假设p_response始终用作callback function,并假设x包含您的网络服务响应:

x.sub(/^\s*p_response\(/, "").sub(/\)\s*$/,"")

这将为您提供干净的JSON来解析,而不是JSONP。