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

时间:2022-02-19 21:27:17

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 = 'bigbadwolf@grannysplace.com,hungry@hippos.com,youtalkin@to.me';

What (I think) it needs to be:

我认为应该是什么:

[
    {"email": "bigbadwolf@grannysplace.com"},
    {"email": "hungry@hippos.com"},
    {"email": "youtalkin@to.me"}
]

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-json But I keep getting syntax errors.

我一直在研究几个jQuery插件,其中包括:http://code.google.com/p/jqueryjson,但我一直在获取语法错误。

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

另一篇关于此的文章建议手工操作:JavaScript关联数组到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的几十年文档已经有一段时间了,并且没有合适的示例。

4 个解决方案

#1


17  

var json = [];
var to = 'bigbadwolf@grannysplace.com,hungry@hippos.com,youtalkin@to.me';
var toSplit = to.split(",");
for (var i = 0; i < toSplit.length; i++) {
    json.push({"email":toSplit[i]});
}

#2


3  

Try changing the loop to this:

尝试将循环改为:

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

#3


3  

How about:

如何:

var to = 'bigbadwolf@grannysplace.com,hungry@hippos.com,youtalkin@to.me',
    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: "bigbadwolf@grannysplace.com"}
Object {email: "hungry@hippos.com"}
Object {email: "youtalkin@to.me"}

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

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

#4


3  

Try this ES6 Version which has better perform code snippet.

尝试这个ES6版本,它可以更好地执行代码片段。

'use strict';

let to = 'bigbadwolf@grannysplace.com,hungry@hippos.com,youtalkin@to.me';

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

console.log(emailList);

#1


17  

var json = [];
var to = 'bigbadwolf@grannysplace.com,hungry@hippos.com,youtalkin@to.me';
var toSplit = to.split(",");
for (var i = 0; i < toSplit.length; i++) {
    json.push({"email":toSplit[i]});
}

#2


3  

Try changing the loop to this:

尝试将循环改为:

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

#3


3  

How about:

如何:

var to = 'bigbadwolf@grannysplace.com,hungry@hippos.com,youtalkin@to.me',
    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: "bigbadwolf@grannysplace.com"}
Object {email: "hungry@hippos.com"}
Object {email: "youtalkin@to.me"}

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

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

#4


3  

Try this ES6 Version which has better perform code snippet.

尝试这个ES6版本,它可以更好地执行代码片段。

'use strict';

let to = 'bigbadwolf@grannysplace.com,hungry@hippos.com,youtalkin@to.me';

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

console.log(emailList);