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

如果要将其作为数组使用json_decode,请添加为第二个参数。true

于 2012-05-08T10:27:40.693 回答