json格式在ruby和rails中的注意事项
2014-09-04 15:46
232 查看
#虚拟网络拓扑的json数据 def topodata #@vnic = Vnic.all #flash.now[:notice] = 'Message sent!' #flash.now[:alert] = 'Message sent!' simple_json = { nodes: [{ name: 'bob', age: "22", awesome: "true" }, { name: 'bob', age: 22, awesome:true }], links: [{ name: 'bob', age: "22", awesome: "true" }, { name: 'bob', age: 22, awesome:true }] } vnet = Vnet.find(:all, :select => 'id,name,vswitch_id') vm = VirtualMachine.find(:all, :select => 'id,hostname,virtual_machine_container_id') vmc = VirtualMachineContainer.find(:all, :select => 'id,hostname') vswitch = Vswitch.find(:all, :select => 'id,uuid,virtual_machine_container_id,vnet_id,name') #vswitch 和 vmc有什么关系? vnic = Vnic.find(:all, :select => 'id,virtual_machine_id,vnet_id') #把所有的name放入nodes数组中 如:{"name":"vswitch1","type":"vswitch"} nodes = Array.new for i in 0..vswitch.size-1 do nodes.push({ name: vswitch[i].name.to_s, group: 1 #"vswitch" }) end for i in 0..vm.size-1 do nodes.push({ name: vm[i].hostname.to_s, group: 2 #"vm" }) end for i in 0..vmc.size-1 do nodes.push({ name: vmc[i].hostname.to_s, group: 3 #"vmc" }) end #储存名字和其在nodes中的位置,为了方便edges找到其位置 如:id_hash["vm2"] = 4 id_hash = Hash.new for i in 0..nodes.size-1 do id_hash[nodes[i][:name]] = i end #edges储存边之间的关系 #先储存vswitch和vm的关系 => vnic links = Array.new for i in 0..vnic.size-1 do links.push({ source: id_hash[vm[vnic[i].virtual_machine_id-1].hostname], target: id_hash[vswitch[vnet[vnic[i].vnet_id-1].vswitch_id-1].name], value: 10, des: "vswitch_to_vm" }) end #再储存vm和vmc的关系 => vm for i in 0..vm.size-1 do links.push({ source: id_hash[vm[i].hostname], target: id_hash[vmc[vm[i].virtual_machine_container_id-1].hostname], value: 5, des: "vm_to_vmc" }) end @alljson = {} @alljson["nodes"] = nodes @alljson["links"] = links #flash.now[:alert] = id_hash[vm[2].hostname] lsjson = { nodes: [ {name:"Myriel",group:1}, {name:"Napoleon",group:1}, {name:"Mlle.Baptistine",group:2}, ], links: [ {source:0,target:1,value:1}, {source:1,target:2,value:8}, {source:2,target:1,value:10}, ] } respond_to do |format| format.html format.json { render json: @alljson } #这里会自动调用to_json end #render json: lsjson #render json: {test: 1} end
ruby关于json格式的调用
首先json格式注意:
1、在nodes后面要紧跟:,不能有空格
2、nodes、name这些地方不能用引号括起来,不然不能用ruby转换为json格式
simple_json = { nodes: [{ name: 'bob', age: "22", awesome: "true" }, { name: 'bob', age: 22, awesome:true }], links: [{ name: 'bob', age: "22", awesome: "true" }, { name: 'bob', age: 22, awesome:true }] } 3、(1)可以直接在controller的方法中直接render,就可以得到json格式的数据,比如
render json: simple_json (2)或者另一种方法也可以,这里的@alljson就是类似上面simple_json格式的数据,当然,如果直接是model里导出来的数据也可以直接用,比如@vnic = Vnic.all
respond_to do |format| format.html format.json { render json: @alljson } #这里会自动调用to_json end 4、直接访问url http://localhost:3000/vnet/topodata.json就可以直接得到json数据了 在rubychina上看到如果在url上想不要后面.json就可以看到json数据的话,就要到route里改resource直接为json数据了, routes里面对特定的resource加上format: :json好像是这样。
相关文章推荐
- Ajax--json(Ajax调用返回json封装代码、格式及注意事项)
- Ruby on Rails迁移时的一些注意事项
- json在jquery1.5.1中后台传值过来时候的格式注意事项
- ruby on rails的一些安装注意事项
- ruby on rails 入门注意事项
- ruby on rails 在model中使用enum的注意事项
- 将JSON格式字符串转成对象、数组、取值等(注意事项)
- Ruby on Rails注意事项讲解
- JSON数据格式注意事项
- 数据处理json格式以及java编写注意事项
- json格式转数组注意事项
- Ruby和Ruby on Rails中解析JSON格式数据的实例教程
- 毕业设计报告格式和注意事项
- php使用js保存的json cookie 字符串,然后解析成数组,需要注意的事项
- Ruby On Rails各版本需注意
- [开发知识点] 使用JQuery 将DataTable 转化成JSON数据,前端无法处理JSON数据的注意事项
- Ruby 1.9.2 中的一些注意事项
- IE下json格式的一小点需要注意的地方
- Excel条件格式注意事项
- rails 比较日期年月、数组、返回hash格式的json数据、rails结构体