ParseJSON not working, and json_decode working

I struggled with this for some time, I have a gated JSON object stored in the database as soon as I receive it. I decrypt it with both PHP and jQuery, PHP works fine, jQuery fails, heres code

var data = $.parseJSON('{"page_name":"Test page","page_title":"Test title","site":"0","page_url":"http:\/\/saulius.epickitdev.com\/test-page","page_type":"1","bg_type":"gradient","background_img_url":"","fill_bg_color":"","background_color":"","background_gradient_top":"5194c4","background_gradient_bottom":"000000","video_type":"yt","youtube_video_url":"","youtube_video_start_from":"","mp4_video_url":"","webm_video_url":"","ogg_video_url":"","width_type":"fixed","width_padding":"10","layout_color":"cc25cc","border_size":"3","border_color":"5a7de6","container_position":"middle","container_position_left":"","container_position_right":"","meta_title":"mtest","meta_description":"mdesc","meta_keyword":"mkey","fb_title":"ftitle","fb_description":"fdesc","header_script":{"1":"<meta name=\"keywords\" content=\"h1\">","2":"<meta name=\"keywords\" content=\"h2\">"},"footer_script":[""],"redirect_url":"","exit_message":""}'); console.log(data); 

Now he tells me that the error is:

SyntaxError: JSON.parse: expected ',' or '}' after the property value in the object in column 766 of row 1 of the JSON data

What would be on the '=' of:

 "<meta name=\"keywords\" content=\"h1\">" 

Does anyone have any clues? I tried different parsers and I am completely stuck with this!

+5
source share
1 answer

You need to double-encode the \ characters in String before \\ .

 var s = '{"page_name":"Test page","page_title":"Test title","site":"0","page_url":"http://saulius.epickitdev.com/test-page","page_type":"1","bg_type":"gradient","background_img_url":"","fill_bg_color":"","background_color":"","background_gradient_top":"5194c4","background_gradient_bottom":"000000","video_type":"yt","youtube_video_url":"","youtube_video_start_from":"","mp4_video_url":"","webm_video_url":"","ogg_video_url":"","width_type":"fixed","width_padding":"10","layout_color":"cc25cc","border_size":"3","border_color":"5a7de6","container_position":"middle","container_position_left":"","container_position_right":"","meta_title":"mtest","meta_description":"mdesc","meta_keyword":"mkey","fb_title":"ftitle","fb_description":"fdesc","header_script":{"1":"<meta name=\\"keywords\\" content=\\"h1\\">","2":"<meta name=\\"keywords\\" content=\\"h2\\">"},"footer_script":[""],"redirect_url":"","exit_message":""}' $.parseJSON(s) 

This is because they exit the line inside the line.

The top-level string is an integer value, so deploying one level of escaping leads to this,

 {"1":"<meta name="keywords" content="h1">"} #Invalid JSON 

This is not true because the quotation marks in the value are no longer escaped since the top-level JavaScript string expands them. When this string literal is read by the JavaScript interpreter, all the characters \ expand to the same level, which means that \" becomes simple. " Then you need to process the JSON string, but it is no longer valid, since the quotation marks that you wanted to avoid are now not protected (in fact, they never disappeared, they just seemed like that). When you use single quotes, you donโ€™t need to escape, " but you can always avoid anything if itโ€™s not a special character, it just doesnโ€™t do anything.

So, in order for the quotes to be escaped after processing the top-level string, you need to exit \ itself.

 var s = '{"1":"<meta name=\\"keywords\\" content=\\"h1\\">"}' console.log(s) //'{"1":"<meta name=\"keywords\" content=\"h1\">"}' $.parseJSON(s) 
+6
source

Source: https://habr.com/ru/post/1200735/


All Articles