简体   繁体   English

json_decode不返回php中的数组

[英]json_decode does not return array in php

I am facing with a problem, when I am trying to parse json returns from server into array in php. 我在尝试将json从服务器返回的结果解析为php中的数组时遇到了一个问题。 Here is my code ... 这是我的代码...

<?php
    mb_internal_encoding('UTF-8');  
    $url = 'http://localhost/busexpress/api/v1/mobile_user_register/mobile_user_register/retrieve.json';
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $data = curl_exec($ch);
    //$data="'".$data."'";
    echo $data;
    curl_close($ch);

    //$trimspace = preg_replace('/\s+/', '', $data); 
    //echo $trimspace;

    $jdata = json_decode($data, true);
    print_r $jdata; 

?>

This is the json after trimming space. 这是修剪空间后的json。 I also want to convert it int array with json_decode() but no result return. 我也想用json_decode()将其转换为int数组,但没有结果返回。 I think this json is valid. 我认为这个json是有效的。 And suggestion pls. 和建议。 This is my firstly trying to feed web service from server. 这是我第一次尝试从服务器提供Web服务。

Thanks 谢谢

 '{
"status": "1",
"user": [        
    {
        "id": "27",
        "name": "kktt",
        "phone_no": "1239293",
        "activate_code": "0d08ed",
        "deposit": "0",
        "created": "2015-06-0316:35:08",
        "updated": "1110-11-3000:00:00",
        "status": "0"
    },
    {
        "id": "28",
        "name": "kktt",
        "phone_no": "1239293",
        "activate_code": "fb4876",
        "deposit": "0",
        "created": "2015-06-0316:37:14",
        "updated": "1000-01-0100:00:00",
        "status": "0"
    }
  ]
}'

----------Edit--------- - - - - - 编辑 - - - - -

As your suggestion I comment trimming space and correct json format. 作为您的建议,我评论修剪空间和正确的json格式。 And echo $data; 并回显$ data; ..... .....

{
"status": "1",
"user": [        
    {
        "id": "27",
        "name": "kktt",
        "phone_no": "1239293",
        "activate_code": "0d08ed",
        "deposit": "0",
        "created": "2015-06-0316:35:08",
        "updated": "1110-11-3000:00:00",
        "status": "0"
    },
    {
        "id": "28",
        "name": "kktt",
        "phone_no": "1239293",
        "activate_code": "fb4876",
        "deposit": "0",
        "created": "2015-06-0316:37:14",
        "updated": "1000-01-0100:00:00",
        "status": "0"
    }
  ]
}

In decoding array doesn't have any data. 在解码数组中没有任何数据。

 $jdata = json_decode($data, true);
 print_r $jdata; 
 echo "user status -> ". $jdata["status"];

when I copy that json and hard code in a string, decode it again, it works for me. 当我将json和硬代码复制到字符串中时,再次对其进行解码,它对我有用。 please see my testing code.... 请参阅我的测试代码。

$data =' {"status":"1","mobile_user":[{"id":"1","name":"saa","phone_no":"09978784963","activate_code":"","deposit":"0","created":"2015-05-29 00:00:00","updated":"0000-00-00 00:00:00","status":"1"},{"id":"3","name":"ttr","phone_no":"090930499","activate_code":"","deposit":"0","created":"2015-06-01 00:00:00","updated":"0000-00-00 00:00:00","status":"0"}]}';
$data = json_decode($data,true);
$status = $data['status'];
$mobile_user = $data['mobile_user'];
$id = $mobile_user[0]["id"];
$name = $mobile_user[0]["name"];
echo "id -> ". $id ."<br>";
echo "name -> ". $name;

Any suggestion pls! 任何建议请!

I think your json is malformed. 我认为您的json格式错误。 Remove $data="'".$data."'"; 删除$data="'".$data."'";

You can check json error if any. 您可以检查json错误(如果有)。

And $trimspace = preg_replace('/\\s+/', '', $data); $trimspace = preg_replace('/\\s+/', '', $data); is needless. 是没有必要的。

Try this 尝试这个

  $jdata = json_decode($trimspace, true);
     print_r($jdata);

json_decode usually returns an object , so I don't think your code is wrong here. json_decode通常返回一个object ,因此我认为您的代码在这里没有错。

$arrayObject = new ArrayObject($object);
$array = $arrayObject->getArrayCopy();

This is how you can convert it to an array . 这是将其转换为array It works in PHP 5.3+ 它适用于PHP 5.3+

First of all your json is malformed. 首先,您的json格式错误。 Remove the '' from the beginning and the end of your file. 从文件的开头和结尾删除“”。 The contents of $data should look like this: $ data的内容应如下所示:

{
"status": "1",
"user": [        
    {
        "id": "27",
        "name": "kktt",
        "phone_no": "1239293",
        "activate_code": "0d08ed",
        "deposit": "0",
        "created": "2015-06-0316:35:08",
        "updated": "1110-11-3000:00:00",
        "status": "0"
    },
    {
        "id": "28",
        "name": "kktt",
        "phone_no": "1239293",
        "activate_code": "fb4876",
        "deposit": "0",
        "created": "2015-06-0316:37:14",
        "updated": "1000-01-0100:00:00",
        "status": "0"
    }
  ]
}

Second $jdata is an associative array. 第二个$ jdata是一个关联数组。 You cannot print its contents with echo. 您不能使用echo打印其内容。 Instead do 相反做

print_r($jdata);

Third you don't need to remove spaces. 第三,您不需要删除空格。 Do that in the script that produces the json, otherwise just parse the json with the spaces directly. 在生成json的脚本中执行此操作,否则只需直接使用空格解析json。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM