There is such an object:

var data = '{ \ "obj": [ \ { "id": 1, "coord": [53.831235, 27.175426], "country": "<div class=\"nameClass\"><\/div>", "city": "Москва", "address": "ΡƒΠ»ΠΈΡ†Π° Π”ΠΎΠ²ΠΆΠ΅Π½ΠΊΠΎ, 1с1" },\ { "id": 2, "coord": [53.918879, 27.906016], "country": "Россия", "city": "Москва", "address": "ΡƒΠ»ΠΈΡ†Π° Π”ΠΎΠ²ΠΆΠ΅Π½ΠΊΠΎ, 2" }\ ]\ }'; 

After translate in json data = JSON.parse(data);

But it gives an error:

Unexpected token <in JSON

. How to add a layout with classes to an object so that json does not swear?

  • So the problem is not layout classes ..... Why so many backslashes? I would personally swear at them. For in this form, of course, it was not valid - Aleksey Shimansky
  • one
    In general, you have 5 unnecessary backslashes. fix it and it will work ..... for validation you can throw json here json.parser.online.fr and remove all unnecessary - Aleksey Shimansky
  • I was thinking of escaping the class cap, but it doesn't work, but how to escape quotes is necessary because the object has quotes. and slashes that are at the end of the line so that you can write on a new line. the error is only in adding layout with the class, how to do it? - Karalahti
  • I repeat the ΠΏΡ€ΠΎΠ±Π»Π΅ΠΌΠ° Π½Π΅ классах вСрстки again ΠΏΡ€ΠΎΠ±Π»Π΅ΠΌΠ° Π½Π΅ классах вСрстки and not in slashes around classes ..... you have 5 slashes in completely different, totally incomprehensible places ....... слэши ΠΊΠΎΡ‚ΠΎΡ€Ρ‹Π΅ Π² ΠΊΠΎΠ½Ρ†Π΅ строки это Ρ‡Ρ‚ΠΎΠ± ΠΌΠΎΠΆΠ½ΠΎ Π±Ρ‹Π»ΠΎ ΠΏΠΈΡΠ°Ρ‚ΡŒ Π½Π° Π½ΠΎΠ²ΠΎΠΉ строкС - for this purpose, concatenation signs were invented - Alexey Shimansky
  • one
    Instead of those slashes, which supposedly should allow a new line of code, you need to write a closing quotation mark (single) and a concatenation character, and on the new line again write an opening quotation mark (single) - Alexey Shimansky

2 answers 2

 var data = '{ \ "obj": [ \ { "id": 1, "coord": [53.831235, 27.175426], "country": "\<div class=\'nameClass\'>\<\/div\>", "city": "Москва", "address": "ΡƒΠ»ΠΈΡ†Π° Π”ΠΎΠ²ΠΆΠ΅Π½ΠΊΠΎ, 1с1" },\ { "id": 2, "coord": [53.918879, 27.906016], "country": "Россия", "city": "Москва", "address": "ΡƒΠ»ΠΈΡ†Π° Π”ΠΎΠ²ΠΆΠ΅Π½ΠΊΠΎ, 2" }\ ]\ }'; var json = JSON.parse(data) 

  • Similarly, on the screen, about the screening <I forgot, it works, thanks! - Karalahti
  • @Karalahti, take a closer look, the error was corrected not by the screening < , but by replacing quotation marks around the attribute with single attributes - Grundy
  • It was worth all the same to indicate what exactly solves the problem. - Grundy

The problem is incorrect quoting of quotes.

Adding \ before quotation mark - screening occurs in the current line.

JSON.parse is trying to parse the resulting string, in which the quotes are no longer escaped .

One solution may be a simple change of quotes for attributes to single ones, since html allows you to use any type of quotes.

Another option: to escape in the source string is not a quotation mark, but a slash itself, then, when parsing JSON.parse, this slash will escape the quotation following it and the problem will disappear.

Example:

 var data = '{ \ "obj": [ \ { "id": 1, "coord": [53.831235, 27.175426], "country": "<div class=\\"nameClass\\">123<\/div>", "city": "Москва", "address": "ΡƒΠ»ΠΈΡ†Π° Π”ΠΎΠ²ΠΆΠ΅Π½ΠΊΠΎ, 1с1" },\ { "id": 2, "coord": [53.918879, 27.906016], "country": "Россия", "city": "Москва", "address": "ΡƒΠ»ΠΈΡ†Π° Π”ΠΎΠ²ΠΆΠ΅Π½ΠΊΠΎ, 2" }\ ]\ }'; console.log(JSON.parse(data)); d.innerHTML = JSON.parse(data).obj[0].country; 
 #d { border: 1px solid red; padding: 5px; } .nameClass { border: 1px solid green; } 
 <div id="d"></div>