0

我需要知道如何提取 JSON 数据,例如下面发布到 php 的 JSON 格式,我试图使用 php 脚本将以下 Json 文件键值数据提取到 mysql。以下 JSON 来自 openalpr,在命令行上使用 alpr 导出为 json

  "version": 2,
  "data_type": "alpr_results",
  "epoch_time": 1594858871000,
  "img_width": 3232,
  "img_height": 3232,
  "processing_time_ms": 522.5,
  "regions_of_interest": [],
  "results": [
    {
      "plate": "QAA1989C",
      "confidence": 90.09095,
      "matches_template": 0,
      "plate_index": 0,
      "region": "my",
      "region_confidence": 0,
      "processing_time_ms": 42.125999,
      "requested_topn": 10,
      "coordinates": [
        {
          "x": 1149,
          "y": 2071
        },
        {
          "x": 1836,
          "y": 2071
        },
        {
          "x": 1836,
          "y": 2268
        },
        {
          "x": 1149,
          "y": 2268
        }
      ]
    }
  ]
}

我一直得到的是这样的错误 Illegal string offset 'result 我测试的用于显示车牌号的 php 代码: 有人可以帮助指导我如何使用 PHP 脚本获取 json 数组中子数组的值吗?谢谢你**

我使用以下代码不确定关于子元素的键值的错误是什么

$filename = "results.json";
          $data = file_get_contents($filename); //Read the JSON file in PHP
          $array = json_decode($data, true); //Convert JSON String into PHP Array
          
          foreach ($data['results']['results'] AS $d)
          {
            echo $d['plate'];
            }
4

1 回答 1

2

您的 json 字符串错误,第一行缺少 {

$json = '
{
    "version": 2,
    "data_type": "alpr_results",
    "epoch_time": 1594858871000,
    "img_width": 3232,
    "img_height": 3232,
    "processing_time_ms": 522.5,
    "regions_of_interest": [],
    "results": [
        {
            "plate": "QAA1989C",
            "confidence": 90.09095,
            "matches_template": 0,
            "plate_index": 0,
            "region": "my",
            "region_confidence": 0,
            "processing_time_ms": 42.125999,
            "requested_topn": 10,
            "coordinates": [
                {
                    "x": 1149,
                    "y": 2071
                },
                {
                    "x": 1836,
                    "y": 2071
                },
                {
                    "x": 1836,
                    "y": 2268
                },
                {
                    "x": 1149,
                    "y": 2268
                }
            ]
        }
    ]
}
';

$data = json_decode($json,true);

foreach ($data['results'] AS $d) {
    echo $d['plate'];
}
于 2020-07-16T02:12:16.077 回答