2017-03-21 5 views
1

Ich habe einen JSON-String wie untenphp json_decode() gibt null

{"cv_url":"http://localhost/kaj/wp-content/uploads/2017/03/Mir-Ruhul-Amin.doc","cv_path":"C:\wamp\www\kaj/wp-content/uploads/2017/03/Mir-Ruhul-Amin.doc"} 

, während sie von PHP json_decode zu entschlüsseln versucht() es mir Nullwert zu geben.

Jede Hilfe wird geschätzt.

Danke

+0

Wie bekommen Sie dieses JSON? Sie kodieren es selbst oder erhalten es zum Beispiel über API? – hassan

+0

Ich bekomme es von json_encode() -Funktion – ruhul080

+0

Wie codierst du dein Array? – hassan

Antwort

2

Das ist, weil Ihr JSON ungültig ist. Sie müssen es entkommen, wie so:

{ 
    "cv_url": "http://localhost/kaj/wp-content/uploads/2017/03/Mir-Ruhul-Amin.doc", 
    "cv_path": "C:\\wamp\\www\\kaj/wp-content/uploads/2017/03/Mir-Ruhul-Amin.doc" 
} 

So Ihre Variable eigentlich sein sollte:

'{"cv_url": "http://localhost/kaj/wp-content/uploads/2017/03/Mir-Ruhul-Amin.doc","cv_path": "C:\\wamp\\www\\kaj/wp-content/uploads/2017/03/Mir-Ruhul-Amin.doc"}' 

für PHP, sie zu entschlüsseln.

JSON entkommen Sie zuerst das Array in PHP einfach codieren kann, oder wenn, dass Sie nicht zusagt können Sie die folgende Funktion verwenden:

/** 
* @param $value 
* @return mixed 
*/ 
function escapeJsonString($value) { 
    $escapers = array("\\", "/", "\"", "\n", "\r", "\t", "\x08", "\x0c"); 
    $replacements = array("\\\\", "\\/", "\\\"", "\\n", "\\r", "\\t", "\\f", "\\b"); 
    $result = str_replace($escapers, $replacements, $value); 
    return $result; 
} 

TIP:

Sie können die Gültigkeit prüfen immer von Ihre json auf Online-Tools wie:

http://jsonlint.com

und

http://www.jsoneditoronline.org/

+0

und wie er konnte entkommen? mach eine vollständige Lösung – hassan