#虚拟网络拓扑的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. 关于JavaScript中Get/Set访问器

    有时候大家可能会纳闷,在使用JavaScript的时候,只需要给一个系统变量赋值就可以触发一系列操作去相应. 但是我们在写Js的时候,修改了一个自定义变量,却连个P都没有.是不是很郁闷呢? 其实,我们 ...

  2. pythonl练习笔记——python线程的GIL

    python线程中的全局解释器锁GIL(Global Interpreter Lock) python-->支持多线程-->同步和互斥-->加锁-->解释器加锁-->解释 ...

  3. FA_资产成批报废(流程)

    2014-06-08 Created By BaoXinjian

  4. java怎样获得JVM可能的总内存,最大内存,以及空暇内存?

    public class Demo2 { public static void main(String[] args) { DecimalFormat df = new DecimalFormat(& ...

  5. Python rpartition() 方法

    描述 Python rpartition() 方法用来根据指定的分隔符将字符串进行分割. 如果字符串包含指定的分隔符,则返回一个3元的元组,第一个为分隔符前面的子字符串,第二个为分隔符本身,第三个为分 ...

  6. 杭电 1800 Flying to the Mars(贪心)

    http://acm.hdu.edu.cn/showproblem.php?pid=1800 Flying to the Mars Time Limit: 5000/1000 MS (Java/Oth ...

  7. echarts geo地图坐标转换为页面Offset坐标

    https://github.com/apache/incubator-echarts/issues/2540 代码示例: // 获取系列 ) // 获取地理坐标系实例 var coordSys = ...

  8. mysql 添加列的索引

    无论哪种模式加入索引.会大幅度增加SELECT速度 索引名:Index_User_Name 栏目名:user_name 索引类型:Nornal 索引方式:BTREE

  9. 关于K8s集群器日志收集的总结

    本文介绍了kubernetes官方提供的日志收集方法,并介绍了Fluentd日志收集器并与其他产品做了比较.最后介绍了好雨云帮如何对k8s进行改造并使用ZeroMQ以消息的形式将日志传输到统一的日志处 ...

  10. android http post

    public static boolean postInfo(String info1, String info2, String info3, ....) { final HttpParams ht ...