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.

Pramod Karandikar
  • 5,289
  • 7
  • 43
  • 68
Praveen JP
  • 105
  • 1
  • 1
  • 13

3 Answers3

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>');
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
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>');
isvforall
  • 8,768
  • 6
  • 35
  • 50
0

try this one :

var object = JSON.parse(string)
JanLeeYu
  • 981
  • 2
  • 9
  • 24