Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/55.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 从JSON Ruby on Rails获取第一个数组值_Ruby On Rails_Arrays_Json - Fatal编程技术网

Ruby on rails 从JSON Ruby on Rails获取第一个数组值

Ruby on rails 从JSON Ruby on Rails获取第一个数组值,ruby-on-rails,arrays,json,Ruby On Rails,Arrays,Json,我从uri调用返回了这个JSON [ "cat", [ "cat", "cat tree", "cat toys", "cat bed", "cat litter", "cat ears", "cat food", "catastrophe", "caterpillar", "catan" ], [ { "nodes": [ {"name": "Pet Suppli

我从uri调用返回了这个JSON

[
  "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"}
      ]
    },
    {}, {}, {}, {}, {}, {}, {}, {}, {}
  ],
  []
]
我试图在RubyonRails中第一个“cat”(斜体显示)之后获得数组。我试过了

a= JSON.parse(doc) 
result = a["cat"]
但它不起作用。我得到一个奇怪的整数错误

任何帮助都将不胜感激

> doc = "..." # the json string
> json = JSON.parse(doc)
> result = json.detect{|e| e.is_a?(Array)}
结果现在是:

=> ["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处的元素是数组
[“猫”、“猫树”、“猫玩具”…]

显然,斜体字不起作用。这就是我想要从上面的json中得到的。[“猫”、“猫树”、“猫玩具”、“猫床”、“猫窝”、“猫耳朵”、“猫粮”、“灾难”、“毛毛虫”、“卡坦”]完美,谢谢!等它允许的时候,我马上就接受。