#虚拟网络拓扑的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. Linux命令-服务管理命令:chkconfig

    chkconfig --list 查看服务自启动状态列表,等同于查看服务列表 设置某一个服务为自启动服务: chkconfig 服务名 on 修改服务的启动级别为3,,5 查看某一个服务时候已经运行了 ...

  2. Android 布局之LinearLayout 子控件weight权重的作用详析

    关于Android开发中的LinearLayout子控件权重android:layout_weigh参数的作用,网上关于其用法有两种截然相反说法: 说法一:值越大,重要性越高,所占用的空间越大: 说法 ...

  3. InsecureRequestWarning: Unverified HTTPS request is being made. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings In

    InsecureRequestWarning: Unverified HTTPS request is being made. Adding certificate verification is s ...

  4. [hihoCoder] #1055 : 刷油漆

    时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 上回说到,小Ho有着一棵灰常好玩的树玩具!这棵树玩具是由N个小球和N-1根木棍拼凑而成,这N个小球都被小Ho标上了不同的数 ...

  5. [svc]cisco ipsec使用证书认证

    基础配置 用的c7200-adventerprisek9-mz.151-4.M2.bin - R1 conf t int f0/0 ip add 202.100.1.1 255.255.255.0 n ...

  6. spring bean autowire自动装配

    转自:http://blog.csdn.net/xiao_jun_0820/article/details/7233139 autowire="byName"会自动装配属性与Bea ...

  7. Ubantu MySQL数据库操作

    用户管理: 1.新建用户: >CREATE USER name IDENTIFIED BY 'ssapdrow'; 2.更改密码: >SET PASSWORD FOR name=PASSW ...

  8. MySQL - 1093异常 - You can't specify target table 't' for update in FROM clause

    有一个表示地区的表,表结构与数据大概如下表. ID NAME PARENT_ID 1 中国 2 广东省 1 3 广州市 2 4 荔湾区 3 5 越秀区 3 6 番禺区 3 7 小谷围街道 6 现为了查 ...

  9. html5 的localstorage

    /** * 向localStorage中设置数据 * @param key 字符串 * @param value 数组 */ function SetDataIntoLocalStorage(key, ...

  10. uC/OS-III 概要

    本章主要对 uC/OS-III 实时操作系统做一些概要介绍,使读者对 uC/OS-III 有个整体的浅 认识,为后面的章节的详细讲解做一个铺垫. 下图是 uC/OS-III 系统从底层到上层的文件结构 ...