我需要从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工具是否可行?
答案 0 :(得分:6)
我是否通过使用大量字符串连接来构建此json的路径
没有。永远不要通过将字符串拼凑在一起来构建JSON。
或使用PHP的内置JSON工具是否可行?
是
在PHP中构建数据结构。有关具体方法取决于您从哪里获取数据。
完成后,将其传递给json_encode
。
答案 1 :(得分:2)
你从不使用字符串函数来构建JSON!
创建一个数组(或stdClass对象)和json_encode()
它。
答案 2 :(得分:2)
使用数组来构造数据,然后使用json_encode将其转换为JSON。 JSON Encode documentation
答案 3 :(得分:1)
<?php echo json_encode($yourArray); ?>
答案 4 :(得分:0)
使用json_decode
,如果您希望将其作为数组添加true
作为第二个参数。