【问题标题】:PHP Complex String Parse, JSON'able?PHP复杂字符串解析,JSON'able?
【发布时间】:2012-05-24 17:47:41
【问题描述】:

所以我有以下 PHP 字符串:

$output = {"playerId":1178,"percentChange":0.1,"averageDraftPosition":260,"percentOwned":0.1,"mostRecentNews":{"news":"Accardo was called up from Columbus on Monday, the Indians' official Twitter feed reports.","spin":"He'll replace Dan Wheeler on the active roster after carrying a 2.76 ERA over 13 appearances with the Clippers to start the season.","date":"Mon May 14"},"fullName":"Jeremy Accardo"}

我需要的是:“印第安人官方推特报道称,周一,阿卡尔多从哥伦布被召来。” “他将在本赛季开始时在快船队出场 13 次以 2.76 的 ERA 取代丹·惠勒。”作为子串。但我似乎无法找出最好和最优雅的方式来做到这一点。我尝试对字符串进行 JSON_decode,但没有返回任何内容。有任何想法吗? (我正在使用 PHP)

【问题讨论】:

  • 你能给我们一个你用来解析它的代码的sn-p吗?另外,请注意 json_decode 有第二个变量,可让您选择它是数组还是对象。
  • @sfgiants2010 您的 json 数据包含许多未转义的单引号,我已修复它,请检查我的答案。

标签: php json string parsing


【解决方案1】:

这不是string。试试这样:

$output = '{"playerId":1178,"percentChange":0.1,"averageDraftPosition":260,"percentOwned":0.1,"mostRecentNews":{"news":"Accardo was called up from Columbus on Monday, the Indians\' official Twitter feed reports.","spin":"He\'ll replace Dan Wheeler on the active roster after carrying a 2.76 ERA over 13 appearances with the Clippers to start the season.","date":"Mon May 14"},"fullName":"Jeremy Accardo"}';

$object = json_decode($output);
$array = json_decode($output, true);
$string = json_encode($array);

【讨论】:

    【解决方案2】:

    你有几个未转义的字符串,这会导致错误。一个简单的格式可以节省您的时间。

    $output = '{
        "playerId":1178,
        "percentChange":0.1,
        "averageDraftPosition":260,
        "percentOwned":0.1,
        "mostRecentNews": {
            "news":"Accardo was called up from Columbus on Monday, the Indians official Twitter feed reports",
            "spin":"Hell replace Dan Wheeler on the active roster after carrying a 2.76 ERA over 13 appearances with the Clippers to start the season.",
            "date":"Mon May 14"
        },
        "fullName":"Jeremy Accardo"
    }';
    $json = json_decode($output);
    

    【讨论】:

      【解决方案3】:

      你试过了吗?

      $output = '{"playerId":1178,"percentChange":0.1,"averageDraftPosition":260,"percentOwned":0.1,"mostRecentNews":{"news":"Accardo was called up from Columbus on Monday, the Indians\' official Twitter feed reports.","spin":"He\'ll replace Dan Wheeler on the active roster after carrying a 2.76 ERA over 13 appearances with the Clippers to start the season.","date":"Mon May 14"},"fullName":"Jeremy Accardo"}';
      
      $array = json_decode($output, true);
      
      echo $array['mostRecentNews']['news'];
      echo $array['mostRecentNews']['spin'];
      

      【讨论】:

        【解决方案4】:

        json_encode 仅使用 UTF8。你在用 utf8 吗?你有语法错误。如果你手动定义 json 变量,它可能是这样的;

        <?php
        $output=<<<JSONSTR
        {"playerId":1178,"percentChange":0.1,"averageDraftPosition":260,"percentOwned":0.1,"mostRecentNews":{"news":"Accardo was called up from Columbus on Monday, the Indians' official Twitter feed reports.","spin":"He'll replace Dan Wheeler on the active roster after carrying a 2.76 ERA over 13 appearances with the Clippers to start the season.","date":"Mon May 14"},"fullName":"Jeremy Accardo"}
        JSONSTR;
        $variable = json_decode($output);
        var_dump($variable);
        ?>
        

        【讨论】:

          猜你喜欢
          • 2016-06-20
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多