#虚拟网络拓扑的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. 连表查询都用Left Join吧 以Windows服务方式运行.NET Core程序 HTTP和HTTPS的区别 ASP.NET SignalR介绍 asp.net—WebApi跨域 asp.net—自定义轻量级ORM C#之23中设计模式

    连表查询都用Left Join吧   最近看同事的代码,SQL连表查询的时候很多时候用的是Inner Join,而我觉得对我们的业务而言,99.9%都应该使用Left Join(还有0.1%我不知道在 ...

  2. RHCE7 管理II-5管理进程的优先级

    进程的优先级值称为进程的nice值,共有40种不同的取值(用数字-20到19表示) nice值越大,表示进程的优先级越低. 进程的nice值,只允许root用户来设置负的nice:其他用户只允许设置正 ...

  3. DBA_实践指南系列7_Oracle Erp R12监控OAM(案例)

    2013-12-07 Created By BaoXinjian

  4. Python 字典 pop() 方法

    描述 Python 字典 pop() 方法删除给定键所对应的键/值对,并返回被删除的值.给定键如果不在字典中,则必须设置一个default值,否则会报错,此时返回的就是default值. 语法 pop ...

  5. Python isupper() 方法

    描述 Python isupper() 方法检测字符串中所有的字母是否都为大写. 相反的方法:islower() 方法. 语法 isupper() 方法语法: S.isupper() 参数 无. 返回 ...

  6. React.Fragment 的作用:代替div作为外层

    https://segmentfault.com/a/1190000013220508 import React, { Fragment } from 'react'; <Fragment> ...

  7. getXXXPos()约定

    class CmyNode:public CCNode{ public: CmyNode(){ m_XXX=NULL; } virtual~CmyNode(){ } bool init(){ m_XX ...

  8. NUMA体系结构详解

    1. NUMA的几个概念(Node,socket,core,thread) 对于socket,core和thread会有不少文章介绍,这里简单说一下,具体参见下图: 一句话总结:socket就是主板上 ...

  9. PHP遍历目录返回统计目录大小实例

    分享一个 PHP遍历目录并返回统计目录大小的方法.代码: <?php $dirname = "test1"; //mkdir($dirname); //遍历一层目录 func ...

  10. C# RSA数据加密

    第一步产生密钥类 CreateKey using System; using System.Collections.Generic; using System.Linq; using System.T ...