1

My string is,

var str =
"tmp_IMG-20160309-WA0008-130273657.jpg,tmp_IMG-20160310-WA00002073543746.jpg,tmp_IMG-20160309-WA000792314756.jpg,tmp_IMG-20160310-WA0002-434051888.jpg";

I need to convert the above string to JSON Object like

[object,object,object]

each object have image name.

Comments highly appreciated.

Thanks you.

0

3 Answers 3

1

I suggest to use String#split() and Array#map() for building an array with objects

var str = "tmp_IMG-20160309-WA0008-130273657.jpg,tmp_IMG-20160310-WA00002073543746.jpg,tmp_IMG-20160309-WA000792314756.jpg,tmp_IMG-20160310-WA0002-434051888.jpg",
    array = str.split(',').map(function (a) {
        return { src: a };
    });

document.write('<pre>' + JSON.stringify(array, 0, 4) + '</pre>');

Sign up to request clarification or add additional context in comments.

Comments

0

try this one :

var object = JSON.parse(string)

Comments

0

var str = "tmp_IMG-20160309-WA0008-130273657.jpg,tmp_IMG-20160310-WA00002073543746.jpg,tmp_IMG-20160309-WA000792314756.jpg,tmp_IMG-20160310-WA0002-434051888.jpg";

var arr = str.split(',').map(e => ({ name: e }));

document.write('<pre>' + JSON.stringify(arr, 0, 2) + '</pre>');

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.