JSON format considerations for Ruby and Rails

Source: Internet
Author: User

  #虚拟网络拓扑的json数据 def topodata # @vnic = vnic.all#flash.now[:notice] = ' Message sent! ' #flash. Now[:alert] = ' Message sent! ' Simple_json = {nodes: [{name: ' Bob ', Age: ' $ ', awesome: ' true '}, {name: ' Bob ', age:22, awesome:true}],links: [{Name : ' Bob ', Age: ' $ ', awesome: ' true '}, {name: ' Bob ', age:22, awesome:true}]}vnet = Vnet.find (: all,: select = = ' Id,na me,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,vir Tual_machine_container_id,vnet_id,name ') What is the relationship between #vswitch and VMC? Vnic = Vnic.find (: All,: Select + = ' id,virtual_machine_id,vnet_id ') #把所有的name放入nodes数组中 such as: {"name": "Vswitch1", "type"       : "vswitch"}nodes = array.newfor i in 0..vswitch.size-1 do Nodes.push ({name:vswitch[i].name.to_s,group:1 # "vswitch" }) ENDfor I in 0..vm.size-1 do Nodes.push ({name:vm[i].hostname.to_s,group:2 # "VM"}) ENDfor i in 0..vmc.size-1 do Nodes.push ({name:vmc[i].hostname.to_s,group:3 # "VMC"}) end# stores the name and its position in the nodes, in order to Edges find its location such as: id_hash["vm2"] = 4id_hash = Hash.newfor i in 0..nodes.size-1 do id_hash[nodes[i][:name]] = i end#edg The relationship between ES storage edges # First stores vswitch and vm relationships = Vniclinks = Array.newfor i in 0..vnic.size-1 do Links.push ({Source:id_hash[vm[vni C[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# the relationship between VM and VMC = vmfor i in 0..vm.size-1 do Links.push ({source:id_hash[vm[i].h Ostname],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 calls in JSON format

First JSON format NOTE:

1, after the nodes to follow:, can not have spaces

2, nodes, name these places cannot be enclosed in quotation marks, otherwise you cannot use Ruby to convert to JSON format.

Simple_json = {nodes: [{name: ' Bob ', Age: ' $ ', awesome: ' true '}, {name: ' Bob ', age:22, awesome:true}],links: [{Name : ' Bob ', Age: ' $ ', awesome: ' true '}, {name: ' Bob ', Age:22, awesome:true}]}

3, (1) can be directly in the Controller's method directly render, you can get JSON format data, such as


(2) or another method can also, here @alljson is similar to the above Simple_json format data, of course, if directly is the model in the data can be directly used, such as @vnic = Vnic.all
    respond_to do |format|      Format.html      Format.json {render json: @alljson}  #这里会自动调用to_json    end


4, direct access to the URL Http://localhost:3000/vnet/topodata.json can directly get JSON data

routes inside of the specific resource plus format: :json It seems that way.

JSON format considerations for Ruby and Rails

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.