我需要從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是否可行?
使用字符串創建JSON並不是最好的方法,您將強制PHP評估字符串以瞭解數據的類型,這是一個關鍵,那就是數據。 使用數組不需要做eval,因爲內部數組知道值的數據類型 –
好吧,我如何在php中構建數據結構?我從數據庫調用中獲取數據。 –
通過創建和分配'Array's。 – Quentin