我有这个JSON从uri调用返回
[
"cat",
[
"cat",
"cat tree",
"cat toys",
"cat bed",
"cat litter",
"cat ears",
"cat food",
"catastrophe",
"caterpillar",
"catan"
],
[
{
"nodes": [
{"name": "Pet Supplies", "alias": "pets"},
{"name": "Home & Kitchen", "alias": "garden"},
{"name": "Women's Clothing", "alias": "fashion-womens-clothing"},
{"name": "Toys & Games", "alias": "toys-and-games"}
]
},
{}, {}, {}, {}, {}, {}, {}, {}, {}
],
[]
]
我试图在ruby on rails中获得第一个"猫"(以斜体显示)之后的数组。我试着
a= JSON.parse(doc)
result = a["cat"]
不起作用。我得到一个奇怪的整数错误
> doc = "..." # the json string
> json = JSON.parse(doc)
> result = json.detect{|e| e.is_a?(Array)}
result
现在:
=> ["cat", "cat tree", "cat toys", "cat bed", "cat litter", "cat ears", "cat food", "catastrophe", "caterpillar", "catan"]
你只需要:
json = JSON.parse(doc)
result = json[1]
数据结构是一个数组。索引0处的元素是字符串"cat"
。索引1处的元素为数组[ "cat", "cat trees", "cat toys", ... ]
。