我有一个JSON对象,其中包含以下内容:
[
{
"_id":"5078c3a803ff4197dc81fbfb",
"email":"user1@gmail.com",
"image":"some_image_url",
"name":"Name 1"
},
{
"_id":"5078c3a803ff4197dc81fbfc",
"email":"user2@gmail.com",
"image":"some_image_url",
"name":"Name 2"
}
]
Run Code Online (Sandbox Code Playgroud)
我想将"_id"键更改为"id",这样就可以了
[
{
"id":"5078c3a803ff4197dc81fbfb",
"email":"user1@gmail.com",
"image":"some_image_url",
"name":"Name 1"
},
{
"id":"5078c3a803ff4197dc81fbfc",
"email":"user2@gmail.com",
"image":"some_image_url",
"name":"Name 2"
}
]
Run Code Online (Sandbox Code Playgroud)
我怎么能用Javascript,jQuery或Ruby,Rails做到这一点?
谢谢.
据维基百科称,猴子补丁是:
一种在不改变原始源代码的情况下扩展或修改动态语言的运行时代码的方法.
来自同一条目的以下陈述使我感到困惑:
在Ruby中,术语monkey patch被误解为对类的任何动态修改,并且通常用作在运行时动态修改任何类的同义词.
我想知道Ruby修补猴子的确切含义.它是在做类似下面的事情,还是其他什么?
class String
def foo
"foo"
end
end
Run Code Online (Sandbox Code Playgroud) 我想做这样的事情:
require 'json'
class Person
attr_accessor :fname, :lname
end
p = Person.new
p.fname = "Mike"
p.lname = "Smith"
p.to_json
Run Code Online (Sandbox Code Playgroud)
可能吗?
所以,我有一个包含多行和多列的表.
<table>
<tr>
<th>Employee Name</th>
<th>Reg Hours</th>
<th>OT Hours</th>
</tr>
<tr>
<td>Employee 1</td>
<td>10</td>
<td>20</td>
</tr>
<tr>
<td>Employee 2</td>
<td>5</td>
<td>10</td>
</tr>
</table>
Run Code Online (Sandbox Code Playgroud)
还有另一张表:
<table>
<tr>
<th>Employee Name</th>
<th>Revenue</th>
</tr>
<td>Employee 2</td>
<td>$10</td>
</tr>
<tr>
<td>Employee 1</td>
<td>$50</td>
</tr>
</table>
Run Code Online (Sandbox Code Playgroud)
请注意,员工订单可能在表之间是随机的.
我如何使用nokogiri创建一个以每个员工为对象的json文件,以及他们的总小时数和收入?
目前,我只能使用一些xpath获取单个表格单元格.例如:
puts page.xpath(".//*[@id='UC255_tblSummary']/tbody/tr[2]/td[1]/text()").inner_text
Run Code Online (Sandbox Code Playgroud)
编辑:
使用页面对象gem和来自@Dave_McNulla的链接,我尝试了这段代码只是为了看看我得到了什么:
class MyPage
include PageObject
table(:report, :id => 'UC255_tblSummary')
def get_some_information
report_element[1][2].text
end
end
puts get_some_information
Run Code Online (Sandbox Code Playgroud)
然而,没有任何东西被归还.
数据:https://gist.github.com/anonymous/d8cc0524160d7d03d37b
小时表有一个副本.第一个很好.需要的另一个表是附件收入表.(我还需要激活表,但我会尝试将合并小时和附件收入表的代码合并.
我想要包含在json字符串中的几个变量,而不是文字.这不起作用:
a1 = get_email
a2 = get_user_name
json1 = '{"email": a1, "username": a2}'
Run Code Online (Sandbox Code Playgroud)
因为不评估变量.
反过来这样做会使json无效,因为json中不允许使用单引号:
a1 = get_email
a2 = get_user_name
json1 = "{'email': a1, 'username': a2}"
Run Code Online (Sandbox Code Playgroud)
如何用这些变量创建一个json?