Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/256.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 需要给定格式的Json_Php_Json_Web Services - Fatal编程技术网

Php 需要给定格式的Json

Php 需要给定格式的Json,php,json,web-services,Php,Json,Web Services,我希望我的web服务使用给定格式的Json: "header":{ "code":"0", "message":"success" }, "body":{ "questions":{ "1":"who is microsoft CEO", "2":"OOP stands for?" }, "answers":[ "ques

我希望我的web服务使用给定格式的Json:

        "header":{
        "code":"0",
        "message":"success"
    },
    "body":{
        "questions":{
            "1":"who is microsoft CEO",
            "2":"OOP stands for?"
        },
        "answers":[
        "question 1":[
            "Bill Gates",
            "Nelson Mandela",
            "NSteve jobs",
            "Robert Andrew",]
         "question 2":[
            "Object Oriented program",
            "Object Oriented programing",
            "Object Oriented proffesion",
            "Object Oriented prefix"
        ]
    }

}
我现在要买这个

    "header":{
        "code":"0",
        "message":"success"
    },
    "body":{
        "questions":{
            "1":"who is microsoft CEO",
            "2":"OOP stands for?"
        },
        "answers":[
            "Bill Gates",
            "Nelson Mandela",
            "NSteve jobs",
            "Robert Andrew",
            "Object Oriented program",
            "Object Oriented programing",
            "Object Oriented proffesion",
            "Object Oriented prefix"
        ]
    }

}
代码是questionAndAnswer.php,一切运行正常,我只需要Nessed循环部分的代码,所以我得到了所需的json

<?php
/**
 * Created by PhpStorm.
 * User: user
 * Date: 6/9/14
 * Time: 3:47 PM
 */

require('../lib.php');


global $db;

$json = array();
$json['header']['code'] = '0';
$json['header']['message'] = 'success';
$json['body'] = '';

$query_1 = "SELECT * FROM `questions`";
$getNumOfRows = $db->ExecuteQuery($query_1);

$res_id = $getNumOfRows->resHandle;
$numberOfRows = mysql_num_rows($res_id);

$count_ques = 0;
$count_ans = 0;
while ($getNumOfRows_FromDb = $getNumOfRows->FetchAsArray()) {
    $question = $getNumOfRows_FromDb['question_descrip'];

    $json['body']['questions'][$getNumOfRows_FromDb['id']] = $question;

    $query_ans = "SELECT * FROM `answers` WHERE question_id=".$getNumOfRows_FromDb['id'].";";

    $getNumOfRows_ans = $db->ExecuteQuery($query_ans);

    while ($getNumOfRows_FromDb_ans = $getNumOfRows_ans->FetchAsArray()) {

        $answers[$count_ans] = $getNumOfRows_FromDb_ans['answer_opt'];
        //$ques_id= $getNumOfRows_FromDb_ans['answer_opt'];
        $json['body']['answers'] = $answers;

        $count_ans++;
    }
    $count_ques++;
}

echo json_encode($json);
?>
请告诉我代码,这样我可以得到我想要的json。
thanx

在循环中执行查询几乎从来都不是一个好主意。使用连接并直接将答案分配给正确的问题键。您的答案可以使用更好的定义。问题1作为关键是毫无意义的。关键是问题,所以只需使用1作为关键,然后使用相同的关键作为问题ID和答案ID。我甚至不认为您想要的JSON结构有那么大的用处。为什么不为正文值[{'question':'question 1 Text','answers':['answer 1','answer 2','answer 3']},{'question':'question 2 Text','answers':['foo','bar']}这样做呢?这将是一个对象数组,每个对象包含问题和相关答案。