从 javascript for 循环创建 JSON 字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6979092/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Create JSON string from javascript for loop
提问by Sthe
I want to create a JSON string from a javascript for loop. This is what I tried to do (which gives me something that looks like a JSON string), but it does not work.
我想从 javascript for 循环创建一个 JSON 字符串。这就是我试图做的(它给了我一些看起来像 JSON 字符串的东西),但它不起作用。
var edited = "";
for(var i=1;i<POST.length-1;i++) {
edited += '"'+POST[i].name+'":"'+POST[i].value+'",';
}
It gives me this:
它给了我这个:
"type":"empty","name":"email-address","realname":"Email Address","status":"empty","min":"empty","max":"empty","dependson":"empty",
This does not work if I try to convert it into a JSON object later.
如果我稍后尝试将其转换为 JSON 对象,这将不起作用。
回答by Digital Plane
Two problems:
两个问题:
- You want an object, so the JSON string has to start with
{
and end with}
. - There is a trailing
,
which may be recognized as invalid.
- 你想要一个对象,因此JSON字符串必须开始
{
和结束}
。 - 有
,
可能被识别为无效的尾随。
It's probably better to use a library, but to correct your code:
使用库可能更好,但要更正您的代码:
- Change
var edited = "";
tovar edited = "{";
to start your JSON string with a{
- Add
edited = edited.slice(0, -1);
after the for loop to remove the trailing comma. - Add
edited += "}";
after the previous statement to end your JSON string with a}
- 更改
var edited = "";
为var edited = "{";
以开头的 JSON 字符串{
edited = edited.slice(0, -1);
在 for 循环之后添加以删除尾随逗号。edited += "}";
在上一条语句之后添加以结束您的 JSON 字符串}
Your final code would be:
您的最终代码将是:
var edited = "{";
for(var i=1;i<POST.length-1;i++) {
edited += '"'+POST[i].name+'":"'+POST[i].value+'",';
}
edited = edited.slice(0, -1);
edited += "}";
Again, it's best to use a library (e.g. JSON.stringify
) by making an object with a for loop, adding properties by using POST[i].name
as a key and POST[i].value
as the value, then using the library to convert the object to JSON.
同样,最好通过使用JSON.stringify
for 循环创建对象来使用库(例如),通过POST[i].name
用作键和POST[i].value
值来添加属性,然后使用库将对象转换为 JSON。
Also, you are starting with index 1
and ending with index POST.length-2
, therefore excluding indices 0
(the first value) and POST.length-1
(the last value). Is that what you really want?
此外,您以 index 开头并以 index1
结尾POST.length-2
,因此不包括索引0
(第一个值)和POST.length-1
(最后一个值)。这真的是你想要的吗?
回答by Praveen Prasad
//dummy data
var post=[{name:'name1',value:1},{name:'name2',value:2}];
var json=[];
for(var i=0;i<post.length;i++)
{
var temp={};
temp[post[i].name]=post[i].value;
json.push(temp);
}
var stringJson = JSON.stringify(json);
alert(stringJson );
回答by Tomas Jansson
Can't you just build up a hash and do toString
on the hash? Something like this:
你不能只是建立一个散列并toString
在散列上做吗?像这样的东西:
var edited = {};
for(var i=0;i<POST.length-1;i++) {
edited[POST[i].name] = POST[i].value;
}
Or maybe JSON.stringify
is what you are looking for: http://www.json.org/js.html
或者也许JSON.stringify
是你正在寻找的:http: //www.json.org/js.html
回答by Im0rtality
You have extra comma in your JSON string. JSON string format: {"JSON": "Hello, World"}
您的 JSON 字符串中有多余的逗号。JSON 字符串格式:{"JSON": "Hello, World"}
var edited = "{";
for(var i=1;i<POST.length-1;i++) {
edited += '"'+POST[i].name+'":"'+POST[i].value+'",';
}
// remove last comma:
edited = edited.substring(0, edited.length-1) + "}";