用php提取每个级别的数据

时间:2015-11-24 12:10:25

标签: php arrays json

以下是JSon数据。

{
        "statusType": "OK",
        "entity": [
            {
            "category": "category1","difficultyLevel": "Easy",
            "quizAnswerChoices": [{"choiceText": "Yes", "choiceTextHash": "c3f1130841b507a4d1e0f45971d990c6ecd25406"}, {"choiceText": "Yes", "choiceTextHash": "c3f1130841b507a4d1e0f45971d990c6ecd25406"}]
            }
        ],
        "entityType": "java.util.ArrayList",
        "status": 200,
        "metadata": {}
    }

我需要解析 - 实体 - quizAnswerChoices(计算项目)

如何检索每个choiceText

5 个答案:

答案 0 :(得分:1)

使用

interface

  TComp1 = class(TComponent)
  private
    FMenu: TPopupMenu;
  protected
    procedure GetChildren(Proc: TGetChildProc; Root: TComponent); override;
  public
    constructor Create(AOwner: TComponent); override;
  published
    property Menu: TPopupMenu read FMenu;
  end;

implementation

  constructor TComp1.Create(AOwner: TComponent);
  begin
    inherited Create(AOwner);
    FMenu := TPopupMenu.Create(Self);
    FMenu.Name := 'Menu1';
    //FMenu.SetSubComponent(True);
  end;

  procedure TComp1.GetChildren(Proc: TGetChildProc; Root: TComponent);
  begin
    Proc(FMenu);
  end;

答案 1 :(得分:1)

使用此

$arry = json_decode($json,true);


 foreach ($arry['entity'] as $ke => $ve) {
       foreach ($ve['quizAnswerChoices'] as $k => $v) {
         print_r($v);
    } 
 }

答案 2 :(得分:0)

如果您执行类似

的json_decode
$data = json_decode(json_data);

然后$ data将是一个可遍历的数组

$data['entity']['quizAnswerChoices'] etc

答案 3 :(得分:0)

让我们考虑一下你在一个名为"$contents"的变量中收到这个json。 你所要做的就是解码它:

$decoded = json_decode($contents, true); //True so, the decoded object will be converted into an ssociative array

print_r($decoded);

答案 4 :(得分:-1)

使用json_decode将json转换为数组

$dataArray = json_decode($json_string, true)
$dataArray['entity']; // entity
$dataArray['entity']['quizAnswerChoices']; // quizAnswerChoices
$dataArray['entity']['quizAnswerChoices']; // quizAnswerChoices
count($dataArray['entity']['quizAnswerChoices']); // quizAnswerChoices count