#虚拟网络拓扑的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 好像是这样。

json格式在ruby和rails中的注意事项的更多相关文章

  1. 【转】Ruby on Rails中select使用方法

    在Ruby on Rails中真的有一堆Select helper可以用,我们经常容易混淆.常见的有三个..select, select_tag, collection_select(其余的什么sel ...

  2. ruby on rails 中render的

    Ruby rails页面跳转代码如下: 1.render(:text => string) 2.render(:inline => string, [:type => "r ...

  3. json格式数据 ,将数据库中查询的结果转换为json(方式2)

    controller: /*** * 返回所有版本的信息,json的形式返回到前台 * @return */ @RequestMapping(value="/getAllVersion&qu ...

  4. ruby on rails 中render的使用

    最近写ror,因为比较菜,很多东西不知道,只能看一点查一点了 render 先上点搜集的常用方式 render :action => "long_goal", :layout ...

  5. Ruby on Rails中的Rake教程(Rake如何把我灌醉!)

    下面是我们使用Rake任务的例子: 1.给列表中的用户发送邮件 2.每晚数据的计算和报告 3.过期或重新生成缓存 4.备份数据和svn版本(how's this : subversion reposi ...

  6. 理解ruby on rails中的ActiveRecord::Relation

    ActiveRecord::Relation是rails3中添加的.rails2中的finders, named_scope, with_scope 等用法,在rails3统一为一种Relation用 ...

  7. asp.net后台cs中的JSON格式变量在前台Js中调用方法(前后台示例代码)

    //后台cs代码: using System; using System.Collections.Generic; using System.Linq; using System.Web; using ...

  8. asp.net后台cs中的JSON格式变量在前台Js中调用方法

    //后台cs代码: using System; using System.Collections.Generic; using System.Linq; using System.Web; using ...

  9. @ResponseBody//该注解会将返回值转为json格式并放到响应体中返回到前台

随机推荐

  1. Python练习笔记——斐波那契数列

    斐波那契数列(Fibonacci sequence),又称黄金分割数列.因数学家列昂纳多·斐波那契(Leonardoda Fibonacci)以兔子繁殖为例子而引入,故又称为“兔子数列”,指的是这样一 ...

  2. redhat 6.4 安装VirtualBox自动增强功能功:unable to find the sources of your current Linux kernel

    redhat 6.4 安装VirtualBox自动增强功能功能的时候提示: building the main Guest Additions module FAILED unable to find ...

  3. Sql 列转行 三种方法对比

    合并列值   --******************************************************************************************* ...

  4. Mybatis里Mapper映射sql文件里insert的主键返回selectKey使用

    有时候新增一条数据,知道新增成功即可,但是有时候,需要这条新增数据的主键,以便逻辑使用,再将其查询出来明显不符合要求,效率也变低了. 这时候,通过一些设置,mybatis可以将insert的数据的主键 ...

  5. ui-router路由控制器(一)

    angularUI 在不断发展过程中已经被划分成了几个模块,你可以选择你需要的模块载入,我们今天要了解一下路由控制器 ui-router ,它就是angularUI划分出出来的一个独立模块. 此模块只 ...

  6. 使用Frame控件设计Silverlight的导航

    这里所说的导航其实就是在Silverlight的页面之间前进后退以及跳转.通过Frame控件配合后台NavigationService类可以很容易的做到页面之间的导航. 这就是工具箱中的Frame控件 ...

  7. if else 的或(||)

    = "node01" ]; then ssh -p2222 root@.xx.xx.xxx else echo "hello world" #当$=='node ...

  8. cocos2d-x解决中文乱码问题的几种办法

    昨天改写cocos2d-x的例程,想在其基础上加上一个计分系统.没有分数实在让人没有玩下去的动力! 我在主场景上加上了一个CCLabelTTF,用于显示分数. 但是意外的发现,当内容含有中文时,CCL ...

  9. ny214 单调递增子序列(二) 动态规划

    单调递增子序列(二) 时间限制:1000 ms  |  内存限制:65535 KB 难度:4 描述 给定一整型数列{a1,a2...,an}(0<n<=100000),找出单调递增最长子序 ...

  10. 【图文教程】WebStorm下使用Github下载以及上传代码

    1.从一个git路径下,下载代码到本地,选择VCS->Checkout from Version Control ->GitHub.        2.可能会弹出需要设置上传代码的密码,这 ...