Hi, I've got this string when parsed a web page.

string(41) "_result_ = {status:"OK",
id:"30025",
name:"John Doe",
location:"India"}"

I believe that this code is JSON. but when I use json_decode() , it returns NULL. Someone Please help me parse this string. Thank you. :)

Recommended Answers

All 4 Replies

remove the "_result_ = " part.
use json_decode() on the rest

Hello pzuurveen, I've done like this. But, still I get NULL output. Can you please check it?

$var = preg_replace("/_result_=/", "", $result);
$var =trim($var,'"'); //quotes Free String
$output = json_decode($var);
var_dump($output);
//Output is NULL

in json the name also has to be inside "", I foreget about that.
Gess that you have to write your own parser.
something like:

    $var = preg_replace("/_result_=/", "", $var);
    $var =trim($var);
    $var =trim($var,'{'); //remove {
    $array = explode (",", $var);
    $result=array();
    foreach ($array as $var2)
        {
        $array2=explode(':',$var2);
        $result[$array2[0]]=$array2[1];
        }  
    var_dump ($result);

Perfect! Thank you very much. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.