如何创建下面给出的json字符串

时间:2016-02-26 13:23:36

标签: php json

我是Android开发人员和PHP新手。我不太了解PHP。我创建了

<?php
header('Content-Type: application/json; charset=utf-8');
$mysqli = new mysqli ('localhost', 'mabhi', '9993', 'general');
//PROBLEM LANGUAGE ?????
if (function_exists('mysql_set_charset')) {
    mysqli_set_charset($mysqli, 'utf8');
} else {
    mysqli_query($mysqli, "SET NAMES 'utf8'");
}
// Check if album id is posted as GET parameter
$myq = $mysqli->query('SELECT * FROM Ages');
while ($myr = $myq->fetch_assoc()) {
    $array["Questions"][] = (array(
        'Question'      => $myr['Question'],
        'Answer'        => $myr['option1'],
        'Answer'        => $myr['option2'],
        'Answer'        => $myr['option3'],
        'Answer'        => $myr['option4'],
        'CorrectAnswer' => $myr['CorrectAnswer'],
    ));
}
echo json_encode($array, JSON_UNESCAPED_UNICODE);
?>

输出:

{
  "Questions": [
    {
      "Question": "sfsa sfd s sdf",
      "Answer": "vvv",
      "CorrectAnswer": null
    },
    {
      "Question": "dsfgdsfgv dsf  dfs",
      "Answer": "vvvv  vv",
      "CorrectAnswer": null
    }
  ]
}

但是我想以下面的格式输出json:每个问题都会多次显示答案。请告诉我我的代码有哪些变化。

{
  "Questions": [
    {
      "Question": "dfsfdsfgv dfsfsd dfs sf",
      "CorrectAnswer": 3,
      "Answers": [
        {
          "Answer": "vvvvvvv"
        },
        {
          "Answer": "vvv"
        },
        {
          "Answer": "vv"
        },
        {
          "Answer": "v"
        }
      ]
    },
    {
      "Question": "dgdsgdsgdsgszdfvgfvds",
      "CorrectAnswer": 0,
      "Answers": [
        {
          "Answer": "Lee"
        },
        {
          "Answer": "Wrangler"
        },
        {
          "Answer": "Levi's"
        },
        {
          "Answer": "Diesel"
        }
      ]
    }
  ]
}

3 个答案:

答案 0 :(得分:0)

你做错的第一件事就是你覆盖了答案选项的价值,所以当第一个选项被第二个选项覆盖时,第二个选项将被第三个和第三个选项覆盖,第四个选项将被覆盖。因此,您必须为答案创建一个数组并推送该数组中的每个答案,最后分配此答案选项数组来回答。所以你的代码应该如下所示

while ( $myr = $myq->fetch_assoc () ) {
   $answers_array=new array();

   $answer_option=new stdClass(); // Create a object to format in required format and use this object to store every option of answer
   $answer_option->answer=$myr['option1'];       
   array_push($answers_array,$answer_option);
   $answer_option->answer=$myr['option2']; 
   array_push($answers_array, $answer_option);
   $answer_option->answer=$myr['option3']; 
   array_push($answers_array, $answer_option);
   $answer_option->answer=$myr['option4']; 
   array_push($answers_array,$answer_option);

  $array["Questions"][] = array(   
  'Question' => $myr['Question'],
  'CorrectAnswer' => $myr['CorrectAnswer'],
  'answers' => $answers_array;
);

}

答案 1 :(得分:0)

您不应该分配具有相同标识符(“答案”)的多个数组元素,因为它们只会相互覆盖。我建议你为Answers使用一个非关联数组,这样你的目标JSON就像这样:

{
  "Questions": [
    {
      "Question": "dfsfdsfgv dfsfsd dfs sf",
      "CorrectAnswer": 3,
      "Answers": [
        "vvvvvvv",
        "vvv",
        "vv",
        "v"
      ]
    },
    {
      "Question": "dgdsgdsgdsgszdfvgfvds",
      "CorrectAnswer": 0,
      "Answers": [
        "Lee",
        "Wrangler",
        "Levi's",
        "Diesel"
      ]
    }
  ]
}

因此,您将使用以下PHP代码进行循环:

$array = array();
while ($myr = $myq->fetch_assoc()) {
    $array["Questions"][] = array(
        'Question'      => $myr['Question'],
        'CorrectAnswer' => $myr['CorrectAnswer'],
        'Answers'       => array(
            $myr['option1'],
            $myr['option2'],
            $myr['option3'],
            $myr['option4'],
        ),
    );
}

答案 2 :(得分:0)

正如jereon所指出的,你正在覆盖Answer键的值。您需要为所有Answers一起创建一个单独的数组。

更改

$array["Questions"][] = (array(   
'Question' => $myr['Question'],
'Answer' => $myr['option1'],
'Answer' => $myr['option2'],
'Answer' => $myr['option3'],
'Answer' => $myr['option4'],
'CorrectAnswer' => $myr['CorrectAnswer'],
));

$array["Questions"][] = (array(   
'Question' => $myr['Question'],
'Answers' => array((object)array('Answer' => $myr['option1']),
                   (object)array('Answer' => $myr['option2']),
                   (object)array('Answer' => $myr['option3']),
                   (object)array('Answer' => $myr['option4'])),
'CorrectAnswer' => $myr['CorrectAnswer'],
));