拆分 JSON 数据,只获取变量名

Split JSON data and only get the variables names

我正在制作一个 CMS,您必须在其中输入您自己的 JSON 数据字符串。 Like this

然后进入 php 文件。但是在那个文件中我想拆分数据字符串

来自这样的东西:

 {"main_object": {"audio":"nl", "title":"Opdracht 1", "vraag":"[0, 1, "a"]", "antwoord"["yes", "no", 0]" }}

至此

["audio", "title", "vraag", "antwoord"]

使用 json_decode,注意,您的 JSON 字符串不正确:

$json_string = '{"main_object": {"audio":"nl", "title":"Opdracht 1", "vraag":[0, 1, "a"], "antwoord":["yes", "no", 0]}}';

// Add TRUE to force an array, not an object
$array = json_decode($json_string, TRUE);
print_r(array_keys($array['main_object']));

它适用于此代码。我试过了。

假设以下 PHP 代码:

 <?php
 $JSONData = '{"main_object": {"audio":"nl", "title":"Opdracht 1", "vraag":"[0, 1, "a"]", "antwoord"["yes", "no", 0]" }}'; //In reality you'll be getting the JSON data from the form rather than assigning it here.

  $arrayData = json_decode($JSONData, true);
  if ($arrayData !== null 
      && is_array($arrayData)
      && isset($arrayData["main_object"])
      && is_array($arrayData["main_object"])) { //JSON was valid and in the correct format
      $usableData = array_keys($arrayData["main_object"]);
      //Do whatever you need to do with your $usableData
  } else {
     //Handle badly formatted data here.
  }