#虚拟网络拓扑的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. Qt WebView改造成 QML App

    这是去年的一个项目,虽然研究出来了,解了一时之需,但随后束之高阁.当时Qt的版本是4.8.现在整理如下: 把QT HTML5 APP改造成 QML App 方案 新建一个QML自定义控件,该控件包含Q ...

  2. RMAN - 发现I/O瓶颈

    大多数操作系统支持异步I/O. 备份到磁盘,异步I/O是一个优势,因为一个服务器进程同时可以执行多个I/O操作:同步I/O必须等上一个I/O操作完成才可以执行下一个I/O操作. 初始化参数disk_a ...

  3. Workflow_工作流的基本概念(概念)

    2014-06-01 Created By BaoXinjian

  4. Concurrency Managed Workqueue(三)创建workqueue代码分析

    一.前言 本文主要以__alloc_workqueue_key函数为主线,描述CMWQ中的创建一个workqueue实例的代码过程. 二.WQ_POWER_EFFICIENT的处理 __alloc_w ...

  5. 清理linux 某个文件夹下面所有的log文件

    #!/bin/sh #目标文件夹下面所有问题 target_dir="/app/" #删除2天前新建的后缀为log的文件 -name "*.log" -exec ...

  6. Nginx(三):日志文件管理

    一.Nginx日志描述 通过访问日志,你可以得到用户地域来源.跳转来源.使用终端.某个URL访问量等相关信息: 通过错误日志,你可以得到系统某个服务或server的性能瓶颈等.因此,将日志好好利用,你 ...

  7. C 指向指针的指针

    #include <stdio.h> int main() { char *cBooks[] = { "C程序设计语言", "C专家编程", &qu ...

  8. Oracle中查询表字段基本信息、主键、外键(整理)

    背景 因为项目某些模块的数据结构设计没有严格按照某规范设计,所以只能从数据库中查询数据结构,需要查询的信息如下:字段名称.数据类型.是否为空.默认值.主键.外键等等. 在网上搜索了查询上述信息的方法, ...

  9. Memcached安装使用和源代码调试

    memcached官网:http://memcached.org/ 一.安装 下载 # wget http://www.memcached.org/files/memcached-1.4.25.tar ...

  10. vim:inoremap命令

    inoremap命令用于映射按键. i代表是在插入模式(insert)下有效 nore表示不递归no recursion,例如:inoremap Y y和inoremap y Y并不会出现无限循环. ...