php - 在 PHP 中解码 JSON

标签 php json decoding

我正在尝试解码 JSON 文件。

下面显示了 JSON 的摘录。为了更准确地描述这个 JSON,它是 3 X 组 JSON 代码。我正在尝试从第一行中提取与“main_train_uid”和“assoc_train_uid”相关的值,例如 G90491 和 G90525。

我一直在尝试复制 stackoverflow 各个部分中显示的代码示例,但失败了

我意识到这应该相对容易,但我就是无法理解。我得到的输出是 Json 解码失败,错误:0。这表明没有任何问题,但我没有得到这些值。我一直对数组和对象感到困惑。我的代码显示在 JSON 提取之后。

{"JsonAssociationV1":{"transaction_type":"Delete","main_train_uid":"G90491","assoc_train_uid":"G90525","assoc_start_date":"2013-09-07T00:00:00Z","location":"EDINBUR","base_location_suffix":null,"diagram_type":"T","CIF_stp_indicator":"O"}}
{"JsonAssociationV1":{"transaction_type":"Delete","main_train_uid":"P20328","assoc_train_uid":"P21318","assoc_start_date":"2013-08-23T00:00:00Z","location":"MARYLBN","base_location_suffix":null,"diagram_type":"T","CIF_stp_indicator":"C"}}
{"JsonAssociationV1":{"transaction_type":"Delete","main_train_uid":"L13077","assoc_train_uid":"L13045","assoc_start_date":"2013-08-23T00:00:00Z","location":"STPANCI","base_location_suffix":null,"diagram_type":"T","CIF_stp_indicator":"C"}}

JSON 片段存储在 json.txt 中

<?php

$file = "json.txt";
$trains = file_get_contents($file);

foreach (explode("\n", $trains) as $line) {

  $train = json_decode($line,true);

  if (is_array($train)) {

    foreach($train as $item=>$value) {

      foreach($value as $entry) {
        echo $entry->main_train_uid;
        echo $entry->assoc_train_uid;

      }
    }   
  } 
}               


if (is_null($json_train)) {
  die("Json decoding failed with error: ". json_last_error());
}

?>

感谢期待 约翰

编辑

感谢巴尔玛

我的新代码如下

<?php
$file = "json.txt";


$trains = file($file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
foreach ($trains as $train) {
$json=json_decode($train,true);

foreach($json as $item=>$value) {
print_r($item);
foreach($value as $entry) {
echo '<br>';
print_r($entry);


}
}   
}   



if (is_null($json)) {
die("Json decoding failed with error: ". json_last_error());
}

?>

我现在得到以下输出(即值),没有错误,这很棒

JsonAssociationV1
Delete
G90491
G90525
2013-09-07T00:00:00Z
EDINBUR

T
OJsonAssociationV1
Delete
P20328
P21318
2013-08-23T00:00:00Z
MARYLBN

T
CJsonAssociationV1
Delete
L13077
L13045
2013-08-23T00:00:00Z
STPANCI

T
C 

但是,我仍然无法挑选出某些单独的值来单独回显(我需要这样做以便稍后将它们放入数据库中)。例如,我仍然无法仅显示 ma​​in_train_uid

的值

此外,由于其中一个值为 NULL,因此似乎将某些值插入下一组 JSON。例如上面输出中显示的 T 和 C

任何进一步的帮助表示赞赏

谢谢

最佳答案

我看到的两个问题:

首先,如果文件以换行符结尾,explode("\n", $trains) 将返回一个以空字符串结尾的数组。当您对该元素调用 json_decode() 时,它将返回 NULLjson_last_error() == 0。尝试:

foreach (array_filter(explode("\n", $trains) as $train)) {
    ...
}

或者,您可以使用 file(),而不是使用 file_get_contents():

$trains = file($file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
foreach ($trains as $train) {
    ...
}

其次,if (is_null($json_train)) 正在测试一个从未设置过的变量。正确的变量是$train

要从 JSON 中获取特定字段,请使用 $value['main_train_uid']

foreach ($trains as $train) {
    $json=json_decode($train,true);
    foreach ($json as $key => $value) {
        echo $key . "<br>" . $value['main_train_uid'] . "<br>";
    }
}

关于php - 在 PHP 中解码 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18541657/

相关文章:

java - 谁对 URLencoding 进行解码?

windows - 如何在不损坏字符的情况下输出exec.Command的输出数据

php - 如何按 MySQL 中不同表的两个日期字段之间的最新更新日期排序

javascript - 如何在访问时在文本字段中获取和插入用户的电话号码

jquery - knockout 无法让过渡发挥作用

javascript - 跟踪具有动态(变化的)数组大小的 JSON 字符串

PHP curl_multi_getcontent 返回 null

php - php 如何在变量中插入双引号

javascript - jQuery 会正确转义我的引号吗?

c# - 如何正确解码重音字符以显示