Solution to JSON string key missing double quotation marks: jsonkey

Source: Internet
Author: User

Solution to JSON string key missing double quotation marks: jsonkey

Solution to JSON string key missing quotation marks


A json string is a string in the form of key: value. A normal key is enclosed by double quotation marks.

For example:

<?php$data = array('name'=>'fdipzone');echo json_encode($data);                        // {"name":"fdipzone"}print_r(json_decode(json_encode($data), true)); //Array ( [name] => fdipzone )?>

However, if the key of the json string lacks double quotation, json_decode will fail.

<?php$str = '{"name":"fdipzone"}';var_dump(json_decode($str, true)); // array(1) { ["name"]=> string(8) "fdipzone" }$str1 = '{name:"fdipzone"}';var_dump(json_decode($str1, true)); // NULL?>

Solution:Determine whether there is a missing double-cited key. If the key is missing, replace it with the regular expression "key" and perform the json_decode operation.

<? Php/** JSON String Parsing without double quotation of compatible keys * @ param String $ str JSON String * @ param boolean $ mod true: Array, false: object * @ return Array/Object */function ext_json_decode ($ str, $ mode = false) {if (preg_match ('/\ w:/', $ str )) {$ str = preg_replace ('/(\ w +):/is', '"$1":', $ str);} return json_decode ($ str, $ mode) ;}$ str = '{"name": "fdipzone"}'; var_dump (ext_json_decode ($ str, true); // array (1) {["name"] => string (8) "fdipzone" } $ Str1 = '{name: "fdipzone"}'; var_dump (ext_json_decode ($ str1, true); // array (1) {["name"] => string (8) "fdipzone"}?>



The regular expression is used to parse JSON objects, and double quotation marks are processed.

Not much nonsense
Directly Add code
Json does not
Js Code
A = '"Vietnam bans Chinese stamps and indicates that the printed Xisha Islands are" Vietnamese territory ""'; var B; B =. replace (/"Vietnamese territory"/, '\ "Vietnamese territory \"'); document. write (B );

How does one solve double quotation marks in json?

{Title: oh, baby, I love you}
Escape
------ Solution --------------------------------------------------------
Since every field has double quotation marks, why not add them after parsing? It's okay to escape first, and the efficiency is not clear.
------ Solution --------------------------------------------------------
First, we need to understand that data processing needs to be strictly controlled from the input time, and correct prompts should be used to guide the user's input.
If you allow users to enter some special characters here, you should perform proper processing when submitting the data, instead of waiting until all the data has been stored, it is unreasonable to process the data again.
Of course, this is just a bit of advice. As for LZ, your problem
Do not place the tasks that can be handed over to the backend, especially when the data volume is large. When the background generates JSON, it converts all the data \;
------ Solution --------------------------------------------------------
Regular text. replace (// gi ,)
------ Solution --------------------------------------------------------
Is the slash escape
The string keyword of js contains 7 characters, including single quotation marks (slashes...
This is a basic problem.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.