如何将 NodeJs 响应转换为嵌套的 json 响应
Posted
技术标签:
【中文标题】如何将 NodeJs 响应转换为嵌套的 json 响应【英文标题】:How to convert NodeJs response to nested json response 【发布时间】:2018-07-29 20:19:49 【问题描述】:我正在使用 Nodejs(ExpressJS) 和 my-SQL 创建一个 REST API。我想要我的响应,比如嵌套的 json 对象,它在我的前端很容易阅读。我尝试使用 for 循环,但它没有给我正确的结果,是否有任何插件可用于格式化此响应或是否有任何简单的方法来执行此嵌套 json 响应。
我的sql查询是
SELECT category.id as category_id, sub_category.id as subcategory_id, product.id as product_id,
category.name as category_name, sub_category.name as subcategory_name,
product.name as product_name
FROM product
JOIN sub_category ON product.sub_category_id = sub_category.id
JOIN category ON sub_category.category_id = category.id
ORDER BY category.id;
我得到的回应就像
[
"category_id": 1,
"subcategory_id": 1,
"product_id": 11,
"category_name": "category 1",
"subcategory_name": "sub category 1",
"product_name": "New Product new"
,
"category_id": 1,
"subcategory_id": 2,
"product_id": 3,
"category_name": "category 1",
"subcategory_name": "sub category 2",
"product_name": "product 3"
,
"category_id": 1,
"subcategory_id": 1,
"product_id": 1,
"category_name": "category 1",
"subcategory_name": "sub category 1",
"product_name": "product 1"
,
"category_id": 1,
"subcategory_id": 1,
"product_id": 2,
"category_name": "category 1",
"subcategory_name": "sub category 1",
"product_name": "product 2"
,
"category_id": 1,
"subcategory_id": 1,
"product_id": 4,
"category_name": "category 1",
"subcategory_name": "sub category 1",
"product_name": "New Product updated"
,
"category_id": 1,
"subcategory_id": 1,
"product_id": 6,
"category_name": "category 1",
"subcategory_name": "sub category 1",
"product_name": "New Product"
,
"category_id": 1,
"subcategory_id": 1,
"product_id": 8,
"category_name": "category 1",
"subcategory_name": "sub category 1",
"product_name": "New Product new"
,
"category_id": 1,
"subcategory_id": 1,
"product_id": 9,
"category_name": "category 1",
"subcategory_name": "sub category 1",
"product_name": "New Product new"
,
"category_id": 1,
"subcategory_id": 1,
"product_id": 10,
"category_name": "category 1",
"subcategory_name": "sub category 1",
"product_name": "New Product new"
,
"category_id": 2,
"subcategory_id": 3,
"product_id": 5,
"category_name": "category 2",
"subcategory_name": "sub category 3",
"product_name": "product 5"
]
我需要喜欢
[
"category_id" : 1,
"category_name" : "product 1",
"data":[
"sub_category_id" : 1,
"sub_category_name" : "product 1",
"data":[
"product_id" : 1,
"product_name" : "product 1"
,
"product_id" : 2,
"product_name" : "product 2"
]
,
"sub_category_id" : 2,
"sub_category_name" : "subcategory 2",
"data":[
"product_id" : 3,
"product_name" : "product 3"
,
"product_id" : 4,
"product_name" : "product 4"
]
]
,
"category_id" : 2 ,
"category_name" : "category 2",
"data":[
"sub_category_id" : 3,
"sub_category_name" : "sub category 3",
"data":[
"product_id" : 5,
"product_name" : "product 5"
,
"product_id" : 6,
"product_name" : "product 6"
]
]
]
是否有任何插件或任何人都可以帮助我解决这个问题。谢谢你
【问题讨论】:
【参考方案1】:根据 Python PEP20 的“平面比嵌套更好”:) 人们宁愿与相反的任务作斗争——他们希望将嵌套的 JSON 数据转换为平面结构,以便能够轻松地操作数据。 Normalizr 是 AFAIK 最流行的用于此任务的库。
如果真的需要嵌套,应该是写正确reduce函数的问题,我建议这样走。
无论如何 - 如果您想使用第三方软件,这可能会有所帮助:https://www.npmjs.com/package/flat-to-nested
【讨论】:
【参考方案2】:检查下面的 repl 链接,使用嵌套的 forEach 实现,但您也可以使用递归。
https://repl.it/@debasish_pothal/nested-json
【讨论】:
以上是关于如何将 NodeJs 响应转换为嵌套的 json 响应的主要内容,如果未能解决你的问题,请参考以下文章
将带有嵌套字典的json响应转换为pandas数据框[重复]
如何将JSON响应转换为Java List-使用Rest Assured进行API测试