简体   繁体   English

php json_decode返回[对象对象]

[英]php json_decode return [object Object]

I have a problem with JSON data in PHP. 我在PHP中遇到JSON数据问题。 I need to use data from this JSON in my SQL statement. 我需要在我的SQL语句中使用来自此JSON的数据。 When I'm trying to debug it with echo(var_dump, or print_r is not working too) command the output with is 当我尝试使用echo(var_dump或print_r也不起作用)命令对其进行调试时,输出为

{"records":"tekst","name":"[object Object]"}

This is a JSON structre: 这是一个JSON结构:

            {
                records: 'tekst',
                name: {
                    imie: 'imie1',
                    nazwisko: 'nazwisko1'
                }
            }

I'm trying to decode this by json_decode() , but I have an error 我正在尝试通过json_decode()对此进行解码,但出现错误

"Warning: json_decode() expects parameter 1 to be string, array given". “警告:json_decode()期望参数1为字符串,给定数组”。

Does anyone know what's wrong? 有人知道怎么了吗?

PHP manual about JSON and the format required: function.json-decode . 有关JSON及其所需格式的PHP手册: function.json-decode basically, double quotes only and names must be quoted. 基本上,仅双引号,并且名称必须加引号。

a demonstration of conversion using PHP. 使用PHP进行转换的演示。

So, you supply the json string that looks like, with the whitespace removed, like this: 因此,您提供的json字符串看起来像,除去了空格,如下所示:

{records:[{id:1,name:'n1'},{id:2,name:'n2'}]}

Which is an object containing an array with two entries that could be arrays or objects. 这是一个包含带有两个条目的数组的对象,两个条目可以是数组或对象。

Except, it is not a valid JSON string as it contains single quotes . 除外, 它不是有效的JSON字符串,因为它包含单引号 And PHP wants all the names in double quotes, as in "id":1. PHP希望所有名称都用双引号引起来,例如“ id”:1。

So, possible PHP code to recreate that, assuming arrays as the inner entries is: 因此,假设数组为内部条目,可能的PHP代码可以重新创建该代码:

$json = new stdClass();
$records = array();
     $entry = array('id' => 1, 'name' => 'n1');
     $records[] = $entry;

     $entry = array('id' => 2, 'name' => 'n2');
     $records[] = $entry;
$json->records = $records;

$jsonEncoded = json_encode($json);

Which, when 'dump'ed looks like: 当“转储”时,其外观如下:

object(stdClass)[1]
  public 'records' => 
    array
      0 => 
        array
          'id' => int 1
          'name' => string 'n1' (length=2)
      1 => 
        array
          'id' => int 2
          'name' => string 'n2' (length=2)

Now, the string that structure produces is: 现在,结构产生的字符串是:

{"records":[{"id":1,"name":"n1"},{"id":2,"name":"n2"}]}

Which looks similar to yours but is not quite the same. 看起来与您相似,但不完全相同。 Note the names in double quotes. 请注意双引号中的名称。

However, if your json string looked the same then PHP could decode it, as is shown below: 但是,如果您的json字符串看起来相同,则PHP可以对其进行解码,如下所示:

$jsonDecoded = json_decode($jsonEncoded);

var_dump($jsonDecoded, 'decoded');

Output: Note all objects... 输出:注意所有对象...

object(stdClass)[2]
  public 'records' => 
    array
      0 => 
        object(stdClass)[3]
          public 'id' => int 1
          public 'name' => string 'n1' (length=2)
      1 => 
        object(stdClass)[4]
          public 'id' => int 2
          public 'name' => string 'n2' (length=2)

We may want arrays instead so use the true as the second parameter in the 'decode' 我们可能需要数组,因此在“解码”中使用true作为第二个参数

$jsonDecoded = json_decode($jsonEncoded, true);

var_dump($jsonDecoded, 'decoded with true switch');

Output: with arrays rather than objects. 输出:使用数组而不是对象。

array
  'records' => 
    array
      0 => 
        array
          'id' => int 1
          'name' => string 'n1' (length=2)
      1 => 
        array
          'id' => int 2
          'name' => string 'n2' (length=2)
string 'decoded with true switch' (length=24)

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

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