Ruby 中 as_json 和 to_json 方法的区别 [英] Difference between as_json and to_json method in Ruby

查看:50
本文介绍了Ruby 中 as_json 和 to_json 方法的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

as_jsonto_json 这两种方法有什么区别.他们一样吗?如果不是,它们之间有什么区别?

What's the difference between the two methods as_json and to_json. Are they same? If not what's the difference between them?

推荐答案

to_json 返回 String.as_json 返回带有字符串键的哈希值.

to_json returns String. as_json returns Hash with String keys.

> { :name => "Konata Izumi", 'age' => 16, 1 => 2 }.to_json
"{\"name\":\"Konata Izumi\",\"age\":16,\"1\":2}"

> { :name => "Konata Izumi", 'age' => 16, 1 => 2 }.as_json
{"name"=>"Konata Izumi", "age"=>16, "1"=>2}

这篇关于Ruby 中 as_json 和 to_json 方法的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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