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,189
  • 7
  • 40
  • 65
Praveen JP
  • 105
  • 1
  • 1
  • 12

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
  • 351,820
  • 24
  • 303
  • 358
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,366
  • 6
  • 34
  • 49
0

try this one :

var object = JSON.parse(string)
JanLeeYu
  • 996
  • 2
  • 9
  • 22