简体   繁体   English

附加2个或更多json字符串

[英]Appending 2 or more json string

I have form where in I dynamically create json strings and append those strings in a hidden field. 我有形式在哪里动态创建json字符串并将这些字符串附加到隐藏字段中。

for example : Imagine I have a hidden field with id as "json_hidden_field" and my dynamically created jsons will look something like below 例如:想象一下,我有一个id为"json_hidden_field"的隐藏字段,我动态创建的jsons将如下所示

var json_str1 = "{ "name" : "foo1" , "value" : "bar1" }"

var json_str2 = "{ "name" : "foo2" , "value" : "bar2" }"

What I am looking for is to append the string in such a way that the resulting output is 我正在寻找的是以产生输出的方式追加字符串

"{ "name" : "foo1" , "value" : "bar1" } , { "name" : "foo2" , "value" : "bar2" }"

And to achieve this , this is what I have written but I am kind of annoyed by the fact that I have to handle that comma in such a way 为了达到这个目的,这就是我所写的内容,但我有点生气,因为我必须以这种方式处理这个逗号

  full_json= $("#json_hidden_field").val().concat($("#json_hidden_field").val() ? "," : "" , json_str2 )


$("#json_hidden_field").val(full_json)

I need help refactoring this piece of code. 我需要帮助重构这段代码。

Thanks 谢谢

var json_str1 = '{ "name" : "foo1" , "value" : "bar1" }'
var json_str2 = '{ "name" : "foo2" , "value" : "bar2" }'

full_json = [json_str1, json_str2].join(',');

Do this: 做这个:

full_json = [json_str1, json_str2].join();

but if you want to keep it valid JSON, then do this: 但如果你想让它保持有效的JSON,那么这样做:

full_json = "[" + [json_str1, json_str2].join() + "]";

Or better, don't create your JSON strings manually. 或者更好的是,不要手动创建JSON字符串。

var json_str1 = { name : "foo1" , value : "bar1" }
var json_str2 = { name : "foo2" , value : "bar2" }

var full_json = JSON.stringify([json_str1, json_str2]);

You could put the json strings in an array, join them, then set this to the json hidden field val. 你可以把json字符串放在一个数组中,加入它们,然后将它设置为json隐藏字段val。

var json_str1 = "{ 'name' : 'foo1' , 'value' : 'bar1' }"
var json_str2 = "{ 'name' : 'foo2' , 'value' : 'bar2' }"
var full_json = [json_str1, json_str2].join(','))

So that full_json equals the string value: 这样full_json等于字符串值:

{ 'name' : 'foo1' , 'value' : 'bar1' },{ 'name' : 'foo2' , 'value' : 'bar2' }

Then just: 然后就是:

$("#json_hidden_field").val(full_json) 

Further to the other suggestions to use an array, to me your question seems to be implying that you'll be building up the final almost-JSON result (the desired output that you specified isn't valid JSON) as part of several other operations, where possibly not all of those operations will occur every time. 对于使用数组的其他建议,对我来说,你的问题似乎暗示你将构建最终的几乎JSON结果(你指定的所需输出是无效的JSON)作为其他几个操作的一部分,可能并非每次都会发生所有这些操作。 If so, I'd suggest creating an empty array to begin with: 如果是这样,我建议创建一个空数组开头:

var output = [];

Then at any point in your code where you need to add some more JSON just add to the end of the array: 然后在代码中你需要添加更多JSON的任何一点只需添加到数组的末尾:

output.push(json_str1);
// OR
output.push('{ "name" : "foo1" , "value" : "bar1" }');

Then finally when you actually want to submit the form containing your hidden field in your submit handler you'd .join() the array and put the resulting string into your hidden field: 最后当你真的要在提交处理程序中提交包含隐藏字段的表单时,你需要.join()数组并将结果字符串放入隐藏字段:

$("#json_hidden_field").val(output.join(","));

As I mentioned above though, your desired output isn't valid JSON - it's almost JSON, but you'd need to add square brackets around it so that it represents an array of objects: 正如我上面提到的,你想要的输出是无效的JSON - 它几乎是JSON,但你需要在它周围添加方括号,以便它代表一个对象数组:

'[{ "name" : "foo1" , "value" : "bar1" } , { "name" : "foo2" , "value" : "bar2" }]'

...in which case you can use: ...在这种情况下你可以使用:

$("#json_hidden_field").val("[" + output.join(",") + "]");
function updateJsonValues(newJson){
var oldJson=$("#json_hidden_field").val().split(',');
oldJson.push(newJson);
$("#json_hidden_field").val(oldJson);
}

call updateJsonValues(newJson) whenever you get new Json string 每当你得到新的Json字符串时,都会调用updateJsonValues(newJson)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM