Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/264.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在PHP中取消一个要嵌套在JSON中的字符串_Php_Json_Backslash - Fatal编程技术网

在PHP中取消一个要嵌套在JSON中的字符串

在PHP中取消一个要嵌套在JSON中的字符串,php,json,backslash,Php,Json,Backslash,我的服务器从真实设备接收JSON数据,然后将其保存在MySQL JSON字段中。不幸的是,制造商的代码中似乎有一个bug,导致部分数据被字符串化 [ "foo", "bar", "info", { "data": "{\"parts\":[{\"id\":1,\"serial\":\"19777\",\&

我的服务器从真实设备接收JSON数据,然后将其保存在MySQL JSON字段中。不幸的是,制造商的代码中似乎有一个bug,导致部分数据被字符串化

[
  "foo",
  "bar",
  "info",
  {
    "data": "{\"parts\":[{\"id\":1,\"serial\":\"19777\",\"type\":\"NONE\",\"key\":\"\"}]}",
    "vendor": "Test",
    "message": "Hello world"
  }
]
运行
json\u decode
即使使用
json\u UNESCAPED\u斜杠
也会产生一个字符串

Array
(
    [0] => foo
    [1] => bar
    [2] => info
    [3] => Array
        (
            [data] => {"parts":[{"id":1,"serial":"19777","type":"NONE","key":""}]}
            [vendor] => Test
            [message] => Hello world
        )

)
但预期的产出是有限的

Array
(
    [0] => foo
    [1] => bar
    [2] => info
    [3] => Array
        (
            [data] => Array
                (
                    [parts] => Array
                        (
                            [0] => Array
                                (
                                    [id] => 1
                                    [serial] => 19777
                                    [type] => NONE
                                    [key] => 
                                )

                        )

                )

            [vendor] => Test
            [message] => Hellow world
        )

)

因为我不能期望他们很快发布固件更新,所以在保存到MySQL之前,我是否可以在整个JSON上运行任何函数来识别字符串化的JSON并重新编码请求,但不会影响没有类似错误的其他消息?

更新:将
$json
中的任何嵌套json数据转换为关联数组:

使用函数
Recurse()
,通过数组
$arr
递归,以查找任何包含json数据的元素,并
json\u decode()
它们以达到预期的输出:

<?php
$arr = json_decode($json, true);
recurse($arr); // json_decode() any json inside $arr

function recurse(array &$arr)
{
    foreach ($arr as $key => &$value) {
        if (is_array($value)) {
            recurse($value);
        } else {
            if (is_object(json_decode($value))) {
                $value = json_decode($value, true); // turn json into assoc array
            }
        }
    }
}

更新:将
$json
中的任何嵌套json数据转换为关联数组:

使用函数
Recurse()
,通过数组
$arr
递归,以查找任何包含json数据的元素,并
json\u decode()
它们以达到预期的输出:

<?php
$arr = json_decode($json, true);
recurse($arr); // json_decode() any json inside $arr

function recurse(array &$arr)
{
    foreach ($arr as $key => &$value) {
        if (is_array($value)) {
            recurse($value);
        } else {
            if (is_object(json_decode($value))) {
                $value = json_decode($value, true); // turn json into assoc array
            }
        }
    }
}
快速解决方案:

$full_array = array_map('json_decode', $array);
如果并非所有参数都是JSON,并且希望使用实际数组而不是stdClass对象,则可能必须执行以下操作:

function json_decode_array($input) { 
  $from_json =  json_decode($input, true);  
  return $from_json ? $from_json : $input; 
}
$full_array = array_map('json_decode_array', $array);
主要答覆:

Google:php json_解码递归

快速解决方案:

$full_array = array_map('json_decode', $array);
如果并非所有参数都是JSON,并且希望使用实际数组而不是stdClass对象,则可能必须执行以下操作:

function json_decode_array($input) { 
  $from_json =  json_decode($input, true);  
  return $from_json ? $from_json : $input; 
}
$full_array = array_map('json_decode_array', $array);
主要答覆:


Google:php json_decode recursive

如果主要关注的是“数据”字段,那么它应该可以工作

$json = '[
  "foo",
  "bar",
  "info",
  {
    "data": "{\"parts\":[{\"id\":1,\"serial\":\"19777\",\"type\":\"NONE\",\"key\":\"\"}]}",
    "vendor": "Test",
    "message": "Hello world"
  },
  {
    "data": {"parts":[{"id":1,"serial":"19777","type":"NONE","key":""}]},
    "vendor": "Test",
    "message": "Hello world"
  }
  ]';

$arr = json_decode($json, true);
$out = array();
foreach($arr as $a)
{
    if(is_array($a))
    {
        if(!is_array($a['data']))
        {
            $a['data'] = json_decode($a['data'], true);
        }
    }
    $out [] = $a;
}

print_r($out);

如果主要关注的是“数据”字段,那么这应该是可行的

$json = '[
  "foo",
  "bar",
  "info",
  {
    "data": "{\"parts\":[{\"id\":1,\"serial\":\"19777\",\"type\":\"NONE\",\"key\":\"\"}]}",
    "vendor": "Test",
    "message": "Hello world"
  },
  {
    "data": {"parts":[{"id":1,"serial":"19777","type":"NONE","key":""}]},
    "vendor": "Test",
    "message": "Hello world"
  }
  ]';

$arr = json_decode($json, true);
$out = array();
foreach($arr as $a)
{
    if(is_array($a))
    {
        if(!is_array($a['data']))
        {
            $a['data'] = json_decode($a['data'], true);
        }
    }
    $out [] = $a;
}

print_r($out);

问题是并非每个记录都有
数据
字段。我可以使用
isset
进行检查,但是我还需要确定字符串是否包含
\”
。问题是不是每个记录都有
数据字段。我可以使用
isset
进行检查,但是我还需要确定字符串是否包含
\“