3

PHP から JSON を作成する必要があります。JSON の構造は単純ではありません。

{ 

    "new" : {
        "checkpoints" : 
            [
                { 
                    "id" : "businessidea",
                    "name" : "business idea",
                    "purpose" : "Form a business idea", 
                    "customer" : 
                        { "questions" : 
                            [

                                { "questionid" : "id1", "questiontitle": "Evaluate size of the market, likely growth vectors and estimate addressable size.", "answers" :
                                  [
                                      {"answertext" : "an answer here", "answerlink": "", "answers": [
                                          {"answertext": "answer text here", "answerlink": "http://google.com"},
                                          {"answertext": "answer text here", "answerlink": "http://google.com"},
                                          {"answertext": "answer text here", "answerlink": "http://google.com"},
                                          {"answertext": "answer text here", "answerlink": "http://google.com"}
                                      ]},
                                      {"answertext" : "an answer here", "answerlink": "", "answers": [
                                          {"answertext": "answer text here", "answerlink": "http://google.com"},
                                          {"answertext": "answer text here", "answerlink": "http://google.com"},
                                          {"answertext": "answer text here", "answerlink": "http://google.com"},
                                          {"answertext": "answer text here", "answerlink": "http://google.com"}
                                      ]},
                                      {"answertext" : "an answer here", "answerlink": "", "answers": [
                                          {"answertext": "answer text here", "answerlink": "http://google.com"},
                                          {"answertext": "answer text here", "answerlink": "http://google.com"},
                                          {"answertext": "answer text here", "answerlink": "http://google.com"},
                                          {"answertext": "answer text here", "answerlink": "http://google.com"}
                                      ]}
                                  ]
                                },
                                { "questionid" : "id2","questiontitle": "Define the needs of the customers and the value we will deliver to the customers - customer pain and our solution", "answers" :
                                  [
                                      {"answertext" : "an answer here", "answerlink": "", "answers": [
                                          {"answertext": "answer text here", "answerlink": "http://google.com"},
                                          {"answertext": "answer text here", "answerlink": "http://google.com"},
                                          {"answertext": "answer text here", "answerlink": "http://google.com"},
                                          {"answertext": "answer text here", "answerlink": "http://google.com"}
                                      ]},
                                      {"answertext" : "an answer here", "answerlink": "", "answers": [
                                          {"answertext": "answer text here", "answerlink": "http://google.com"},
                                          {"answertext": "answer text here", "answerlink": "http://google.com"},
                                          {"answertext": "answer text here", "answerlink": "http://google.com"},
                                          {"answertext": "answer text here", "answerlink": "http://google.com"}
                                      ]},
                                      {"answertext" : "an answer here", "answerlink": "", "answers": [
                                          {"answertext": "answer text here", "answerlink": "http://google.com"},
                                          {"answertext": "answer text here", "answerlink": "http://google.com"},
                                          {"answertext": "answer text here", "answerlink": "http://google.com"},
                                          {"answertext": "answer text here", "answerlink": "http://google.com"}
                                      ]}
                                  ]
                                 },
                                { "questionid" : "id3","questiontitle": "Define the competitor landscape" , "answers" :
                                  [
                                      {"answertext" : "an answer here", "answerlink": "", "answers": [
                                          {"answertext": "answer text here", "answerlink": "http://google.com"},
                                          {"answertext": "answer text here", "answerlink": "http://google.com"},
                                          {"answertext": "answer text here", "answerlink": "http://google.com"},
                                          {"answertext": "answer text here", "answerlink": "http://google.com"}
                                      ]},
                                      {"answertext" : "an answer here", "answerlink": "", "answers": [
                                          {"answertext": "answer text here", "answerlink": "http://google.com"},
                                          {"answertext": "answer text here", "answerlink": "http://google.com"},
                                          {"answertext": "answer text here", "answerlink": "http://google.com"},
                                          {"answertext": "answer text here", "answerlink": "http://google.com"}
                                      ]},
                                      {"answertext" : "an answer here", "answerlink": "", "answers": [
                                          {"answertext": "answer text here", "answerlink": "http://google.com"},
                                          {"answertext": "answer text here", "answerlink": "http://google.com"},
                                          {"answertext": "answer text here", "answerlink": "http://google.com"},
                                          {"answertext": "answer text here", "answerlink": "http://google.com"}
                                      ]}
                                  ]}
                            ]
                        },

これを達成するための最良の方法は何ですか?多くの文字列連結を使用してこの json を構築するルートをたどりますか、それとも PHP の組み込み JSON ツールを使用することは可能ですか?

4

5 に答える 5

6

多くの文字列連結を使用して、このjsonを構築するルートをたどりますか?

いいえ。文字列を組み合わせて JSON を構築しないでください。

または、PHP のビルトイン JSON ツールを使用することは可能ですか?

はい。

PHP でデータ構造を構築します。そもそもどこからデータを取得するかによって、その方法の詳細が異なります。

完成したら に通すjson_encode

于 2012-05-08T10:27:31.473 に答える
2

JSON を構築するために文字列関数を使用することはありません。

配列 (または stdClass オブジェクト) とjson_encode()それを作成します。

于 2012-05-08T10:27:41.207 に答える
2

配列を使用してデータを構造化し、json_encode を使用して JSON に変換します。JSON エンコードのドキュメント

于 2012-05-08T10:28:17.167 に答える
1
<?php echo json_encode($yourArray); ?>
于 2012-05-08T10:27:23.860 に答える
0

を使用し、配列として使用する場合は 2 番目のパラメーターとしてjson_decode追加します。true

于 2012-05-08T10:27:40.693 に答える