jQuery:将带有逗号分隔值的字符串转换为特定的 JSON 格式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18534906/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 21:53:05  来源:igfitidea点击:

jQuery: Convert string with comma separated values to specific JSON format

javascriptjqueryjson

提问by chocolata

I've been losing hours over something that might be trivial:

我一直在为一些可能微不足道的事情浪费时间:

I've got a list of comma-separated e-mail addresses that I want to convert to a specific JSON format, for use with the Mandrill API (https://mandrillapp.com/api/docs/messages.JSON.html)

我有一个逗号分隔的电子邮件地址列表,我想将这些地址转换为特定的 JSON 格式,以用于 Mandrill API ( https://mandrillapp.com/api/docs/messages.JSON.html)

My string:

我的字符串:

var to = '[email protected],[email protected],[email protected]';

What (I think) it needs to be:

什么(我认为)它需要是:

[
    {"email": "[email protected]"},
    {"email": "[email protected]"},
    {"email": "[email protected]"}
]

I've got a JSFiddle in which I almost have it I think: http://jsfiddle.net/5j8Z7/1/

我有一个 JSFiddle,我认为我几乎拥有它:http: //jsfiddle.net/5j8Z7/1/

I've been looking into several jQuery plugins, amongst which: http://code.google.com/p/jquery-jsonBut I keep getting syntax errors.

我一直在研究几个 jQuery 插件,其中包括:http://code.google.com/p/jquery-json但我不断收到语法错误。

Another post on SO suggested doing it by hand: JavaScript associative array to JSON

SO 上的另一篇文章建议手动完成:JavaScript associative array to JSON

This might be a trivial question, but the Codecadamy documentation of the Mandrill API has been down for some time and there are no decent examples available.

这可能是一个微不足道的问题,但是 Mandrill API 的 Codecadamy 文档已经关闭了一段时间,并且没有像样的示例可用。

回答by Anton

var json = [];
var to = '[email protected],[email protected],[email protected]';
var toSplit = to.split(",");
for (var i = 0; i < toSplit.length; i++) {
    json.push({"email":toSplit[i]});
}

回答by Venkat.R

Try this ES6 Version which has better perform code snippet.

试试这个具有更好执行代码片段的 ES6 版本。

'use strict';

let to = '[email protected],[email protected],[email protected]';

let emailList = to.split(',').map(values => {
    return {
        email: values.trim(),
    }
});

console.log(emailList);

回答by Raúl Juárez

Try changing the loop to this:

尝试将循环更改为:

    var JSON = [];
    $(pieces).each(function(index) {
        JSON.push({'email': pieces[index]});   
    });

回答by tymeJV

How about:

怎么样:

var to = '[email protected],[email protected],[email protected]',
    obj = [],
    parts = to.split(",");

for (var i = 0; i < parts.length; i++) {
    obj.push({email:parts[i]});
}

//Logging
for (var i = 0; i < obj.length; i++) {
    console.log(obj[i]);
}

Output:

输出:

Object {email: "[email protected]"}
Object {email: "[email protected]"}
Object {email: "[email protected]"}

Demo: http://jsfiddle.net/tymeJV/yKPDc/1/

演示:http: //jsfiddle.net/tymeJV/yKPDc/1/