1

I have Strings which look like this one, but with different Id.

[{"Id":33,"Title":"Sweden, Stockholm - Järfälla: Dienstag, 31. Januar 2017 - Mittwoch, 1. Februar 2017"}]

How can I split them so that they become:

Sweden, Stockholm - Järfälla: Dienstag, 31. Januar 2017 - Mittwoch, 1. Februar 2017

I know that I can represent the quotation marks using \", but I don't know how to apply the split or String.prototype.split function in this case.

I tried

var text = "[{\"Id\":33,\"Title\":\"Sweden, Stockholm - Järfälla: Dienstag, 31. Januar 2017 - Mittwoch, 1. Februar 2017\"}]".replace("[{\"Id\":33,\"Title\":\"", '');


alert(text);

But that would still leave "}] at the end and besides, the Strings have different ID's so that would only work for this case.

Thanks in advance!

stepbysteptomathpro
  • 455
  • 1
  • 7
  • 16

2 Answers2

2

Use JSON.parse

var text = "[{\"Id\":33,\"Title\":\"Sweden, Stockholm - Järfälla: Dienstag, 31. Januar 2017 - Mittwoch, 1. Februar 2017\"}]"
console.log(JSON.parse(text)[0].Title.split(","))
Anurag Srivastava
  • 14,077
  • 4
  • 33
  • 43
2

This is just a JSON-string right? So, no splitting needed

const x = JSON.parse('[{"Id":33,"Title":"Sweden, Stockholm - Järfälla: Dienstag, 31. Januar 2017 - Mittwoch, 1. Februar 2017"}]');
// now x is an Array
console.log(x);
// it's first element is an Object
// and you can extract the 'Title' property
console.log(x[0].Title);
.as-console-wrapper { top: 0; max-height: 100% !important; }
KooiInc
  • 119,216
  • 31
  • 141
  • 177