Home  >  Article  >  Backend Development  >  这样的JSON应该 如何写

这样的JSON应该 如何写

WBOY
WBOYOriginal
2016-06-13 12:19:191125browse

这样的JSON应该 怎么写?
数据库数据如下:
我想写成这样的JSON:
[
    {
        "id": "4",
        "name": "天天有鱼",
        "sort": "2",
        "dishes": [
            {
                "id": "5",
                "Name": "清蒸桂鱼",
                "Price": "100",
                "Discount": "124",
                "ParentID_id": "4",
                "Image": "abc.jpg",
                "Unit": "例",
                "Sale": "80",
                "Stock": 999
            },
            {
                "id": "6",
                "Name": "酸汤桂鱼",
                "Price": "50",
                "Discount": "57",
                "ParentID_id": "4",
                "Image": "a12.jpg",
                "Unit": "例",
                "Sale": "50",
                "Stock": 999
            }
        ]
    },
    {
        "id": "7",
        "name": "天天有鱼",
        "sort": "1",
        "dishes": [
            {
                "id": "8",
                "Name": "生拌凉菜",
                "Price": "60",
                "Discount": "80",
                "ParentID_id": "7",
                "Image": "dg.jpg",
                "Unit": "例",
                "Sale": "30",
                "Stock": 999
            }
        ]
    }
]
应该 如何写?
------解决思路----------------------
一般要用递归
无论是递归还是不递归,精华区都有相关的帖子。自己去看一下

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn