我有以下值“字符串像”,我想更新里面的一些值, 我该怎么做?
这是我在调试器中看到的方式
energygrps = W WF Protein DPPC
; Neighborsearching and short-range nonbonded interactions
nstlist = 10
ns_type = grid
pbc = xyz
rlist = 1.3
; Electrostatics
coulombtype = shift
rcoulomb = 1.2
vdw-type = shift
rvdw-switch = 0.9
tau_t = 1.0; 1.0 1.0
ref_t = 300; 300 300
; Pressure coupling is on for NPT
Pcoupl = Berendsen ; Parrinello-Rahman
Pcoupltype = semiisotropic
tau_p = 1.1 1.1
compressibility = 1e-05 1e-05
ref_p = 1.0 1.0
; Free energy control stuff
free_energy = yes
init_lambda = 0.0
delta_lambda = 0
foreign_lambda = 0.05
sc-alpha = 1.3
sc-power = 1.0
sc-sigma = 0.47
couple-moltype = Protein ; name of moleculetype to decouple
couple-lambda0 = vdw-q ; only van der Waals interactions
couple-lambda1 = none ; turn off everything, in this
vdW
couple-intramol = yes
nstdhdl = 10
; Do not generate velocities
gen_vel = no
; options for bonds
constraints = h-bonds ; we only have C-H bonds here
; Type of constraint algorithm
constraint-algorithm = lincs
; Constrain the starting configuration
; since we are continuing from NPT
continuation = yes
lincs-order = 12
ld_seed =-1
我需要将key的值更改为5我应该怎么做(假设我有更多的键)。
我尝试使用 var aa = "'[{key:10,key2:20}]'\r"
并收到错误
我也尝试JSON.parse(aa)
;这也不起作用,
任何想法如何克服这个?
答案 0 :(得分:3)
您无法使用\r
进行解析,因为它不是有效的json字符串。首先替换它,然后尝试解析(注意键是"
):
var aa = '[{"key":10,"key2":20}]\r'.replace("\r", "");
JSON.parse(aa);
见小提琴:
答案 1 :(得分:1)
您提供的字符串看起来像一些控制台日志,它不是有效的json字符串。但它可以转化为有效的。
这适用于一些可能符合您需要的简单案例。
var aa = "'[{key:10,key2:20}]'\r";
var lead = aa.match(/^\s*['"]/)[0];
var trail = aa.match(/['"]\s*$/)[0];
aa = aa.substr(lead.length, aa.length - trail.length - 1);
aa = aa.replace(/(\w+):/g, '"$1":');
var json = JSON.parse(aa);
json[0].key = 5;
var log = document.getElementById('log');
log.innerText = lead + JSON.stringify(json) + trail;
log.innerText += '\ntrail.length = ' + trail.length;
<pre id="log"></pre>
代码已更新。好吧,我知道你没有看到'\ r'的故事。别担心,它仍然存在,因为你可以看到路径的长度是2。
答案 2 :(得分:0)
如果您无法控制输入,则可能不得不使用eval()
(尽管出于安全原因我强烈不鼓励它)。如果可能的话,最好找到一种方法来获取原始输入以确认JSON。
话虽如此,这对我有用:
// Original string
var aa = "'[{key:10,key2:20}]'\r";
// First, cleanup the trailing \r
aa = aa.trim(); // may need to polyfill `.trim()`
// Need to get it to a JSO (JavaScript object)
var temp;
// Assign temp the value of your string.
eval('temp='+aa);
// next, evaluate that string as a JSO.
temp = eval(temp);
// make your changes
temp[0].key = 5;
// restore aa
aa = "'" + JSON.stringify(temp) + "'\r";
// output to the <pre> for visibility
document.getElementById('out').textContent = aa;
<pre id="out"></pre>
答案 3 :(得分:0)
根据输入的方差,普通字符串方法可能是合适的。但是,这取决于可能使用的键的类型,并且字符串操作会剥离16:41:27.526 INFO - Executing: [send keys: null null, [1]])
16:41:27.527 WARN - Exception thrown
java.lang.NullPointerException
at org.openqa.selenium.remote.server.handler.SendKeys.call(SendKeys.java:49)
at org.openqa.selenium.remote.server.handler.SendKeys.call(SendKeys.java:1)
at java.util.concurrent.FutureTask.run(Unknown Source)
at org.openqa.selenium.remote.server.DefaultSession$1.run(DefaultSession.java:168)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
16:41:27.530 WARN - Exception: null
16:41:27.658 INFO - Executing: [delete session: 157e0397-52e0-4d03-b8ee-aef453cd83a2])
16:41:28.843 INFO - Done: [delete session: 157e0397-52e0-4d03-b8ee-aef453cd83a2]
。
\r