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!

4
  • 2
    have you tried to parse the string as json? Commented Apr 20, 2020 at 14:49
  • 2
    You probably want to do a JSON.parse and grab the string from the Title property. Commented Apr 20, 2020 at 14:49
  • 2
    You accepted the answer which didn't address your original question - about splitting Commented Apr 20, 2020 at 15:08
  • 2
    @AnuragSrivastava Thanks for the notification. I marked yours as correct Commented Apr 20, 2020 at 15:16

2 Answers 2

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(","))

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

Comments

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; }

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.