如何使用Ruby on Rails解析JSON? [英] How do I parse JSON with Ruby on Rails?

查看:65
本文介绍了如何使用Ruby on Rails解析JSON?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种解析JSON,提取值并将其写入Rails数据库中的简单方法.

I'm looking for a simple way to parse JSON, extract a value and write it into a database in Rails.

我特别想要的是一种从bit.ly API返回的JSON中提取shortUrl的方法:

Specifically what I'm looking for, is a way to extract shortUrl from the JSON returned from the bit.ly API:

{
  "errorCode": 0,
  "errorMessage": "",
  "results":
  {
    "http://www.foo.com":
    {
       "hash": "e5TEd",
       "shortKeywordUrl": "",
       "shortUrl": "http://bit.ly/1a0p8G",
       "userHash": "1a0p8G"
    }
  },
  "statusCode": "OK"
}

然后使用shortUrl并将其写入与长网址相关联的ActiveRecord对象中.

And then take that shortUrl and write it into an ActiveRecord object associated with the long URL.

这是我可以在概念上完全思考的事情之一,当我坐下来执行时,我意识到我需要学习很多东西.

This is one of those things that I can think through entirely in concept and when I sit down to execute I realize I've got a lot to learn.

推荐答案

这些答案有些过时了.因此,我给你:

These answers are a bit dated. Therefore I give you:

hash = JSON.parse string

Rails应该自动为您加载json模块,因此您不需要只需添加require 'json'.

Rails should automagically load the json module for you, so you don't need to add require 'json'.

这篇关于如何使用Ruby on Rails解析JSON?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆