PHP processing JSON string key missing double quote resolution, jsonkey_php Tutorial

Source: Internet
Author: User
Tags learn php learn php programming

PHP processing JSON string key missing double quote resolution, Jsonkey


This article describes the PHP processing JSON string key missing quotes solution, share for everyone for reference. Here's how:

Typically, JSON strings are strings in the form of key:value, and normal keys are enclosed in double quotes.

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 is missing a double-quoted, then Json_decode will fail.

<?php$str = ' {' name ': ' Fdipzone '} '; Var_dump (Json_decode ($str, true)); Array (1) {["Name"]=> string (8) "Fdipzone"} $str 1 = ' {name: "Fdipzone"} '; Var_dump (Json_decode ($str 1, true)); Null?>

WORKAROUND: Determine if there is a missing double-quoted key, if missing the first to replace with "key", then the Json_decode operation.

<?php/** compatible key does not have a double-quoted JSON string parsing * @param string $str JSON String * @param boolean $mod true:array,false:object* @return Arra Y/object*/function Ext_json_decode ($str, $mode =false) {  if (preg_match ('/\w:/', $str)) {    $str = preg_replace (' /(\w+):/is ', ' "$": ', $str);  }  Return Json_decode ($str, $mode);} $str = ' {' name ': ' Fdipzone '} '; Var_dump (Ext_json_decode ($str, true)); Array (1) {["Name"]=> string (8) "Fdipzone"} $str 1 = ' {name: "Fdipzone"} '; Var_dump (Ext_json_decode ($str 1, true)); Array (1) {["Name"]=> string (8) "Fdipzone"}?>

I hope this article will be helpful for you to learn PHP programming.


JSON value contains double quotes, how to handle

Replace "\" with "".

The notation of regular expressions for parsing JSON objects, and the processing of double quotes

No more nonsense.
Directly on the code
JSON does not
JS Code
A = ' "Vietnam banned Chinese stamps saying the Paracel Islands are" Vietnamese territory "," var b;b = A.replace (/"Vietnamese territory"/, ' \ \ "Vietnamese territory \" ');d Ocument.write (b);

http://www.bkjia.com/PHPjc/879727.html www.bkjia.com true http://www.bkjia.com/PHPjc/879727.html techarticle PHP processing JSON string key missing double quotation mark solution, Jsonkey This article describes the PHP processing JSON string key missing quotes solution, share for everyone for reference. ...

  • Related Article

    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.