2012-05-08 78 views
3

我需要從PHP構建一些JSON。在JSON的結構是小於瑣碎:使用php構建複雜的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的路線,或者使用內置JSON工具的PHP是否可行?

回答

6

難道我用大量的字符串連接的

號切勿串糖化共築JSON下井建設這個JSON的路線。

還是可行的使用PHP的內置JSON工具?

是。

在PHP中構建數據結構。具體的做法取決於你從哪裏獲取數據。

完成後,通過json_encode

+0

使用字符串創建JSON並不是最好的方法,您將強制PHP評估字符串以瞭解數據的類型,這是一個關鍵,那就是數據。 使用數組不需要做eval,因爲內部數組知道值的數據類型 –

+0

好吧,我如何在php中構建數據結構?我從數據庫調用中獲取數據。 –

+0

通過創建和分配'Array's。 – Quentin

1
<?php echo json_encode($yourArray); ?> 
0

使用json_decode,將true作爲第二個參數添加,如果您希望它作爲數組。

2

從來沒有使用字符串函數來構建JSON!

創建一個數組(或stdClass對象)和json_encode()它。

+0

除非您爲沒有好語言的語言編寫JSON序列化程序,否則沒有理由這樣做。 – ThiefMaster

+0

經常說永遠。解決問題的一些方法是錯誤的。 (以及邊緣案例,比如「我們昨天需要它,沒有時間將PHP升級到遠程現代化的東西」應該被尖叫起來並儘快修復,黑客被認爲是黑客)。 – Quentin