-1

私はプロジェクトの途中で、目標を達成するために json を使用する必要があります。json_encode() を使用して mysql と php を使用しています。これを取得するにはどうすればよいですか:

{"contacts": [ { "id": "c200","name": "Ravi Tamada","email": "ravi@gmail.com","address": "xx-xx-xxxx,x - street, x - country","gender" : "male","phone": { "mobile": "+91 0000000000","home": "00 000000","office": "00 000000"}},{ "id": "c201","name": "Johnny Depp","email": "johnny_depp@gmail.com","address": "xx-xx-xxxx,x - street, x - country","gender" : "male","phone": {"mobile": "+91 0000000000","home": "00 000000","office": "00 000000"}},

このように見えるには?

{
"contacts": [
    {
            "id": "c200",
            "name": "Ravi Tamada",
            "email": "ravi@gmail.com",
            "address": "xx-xx-xxxx,x - street, x - country",
            "gender" : "male",
            "phone": {
                "mobile": "+91 0000000000",
                "home": "00 000000",
                "office": "00 000000"
            }
    },
    {
            "id": "c201",
            "name": "Johnny Depp",
            "email": "johnny_depp@gmail.com",
            "address": "xx-xx-xxxx,x - street, x - country",
            "gender" : "male",
            "phone": {
                "mobile": "+91 0000000000",
                "home": "00 000000",
                "office": "00 000000"
            }
    }
            ]
};

複数のチュートリアルで、このhttp://api.androidhive.info/contacts/のようにオブジェクトを「エコー」すると、json オブジェクトがこのように見えるため、それが可能であることはわかっています。間違った関数を使用していますか? これが私のコードです。

$employee = array();
while($employee = mysql_fetch_array($result, MYSQL_ASSOC)) {
$employee[] = array('employee'=>$employee);
}

$output = json_encode(array('employee' => $employee));

}
4

1 に答える 1

2

マニュアルページのJSON_PRETTY_PRINT名前に従ってオプションを設定します。

$output = json_encode(array('employee' => $employee), JSON_PRETTY_PRINT);

ただし、これは本番環境にはお勧めしません。ファイル サイズが大きくなります。

于 2012-08-10T09:07:18.320 に答える