Solved - 如何在react jsx中显示复杂的JSON字符串? How to display complex/nested json string in React jsx?

本文探讨了如何在React应用中处理和显示包含换行符的复杂JSON字符串。建议在前端处理字符串,并使用JSON.parse转换字符串为JSON对象,再用JSON.stringify生成美化后的格式。通过这种方法,可以避免解析错误并展示整洁的JSON布局。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. Handle the String to JSON in Backend? Frontend?

  • Frontend is better in my case, frontend need the raw data, by the way, in backend, we may need to import json-lib dependencies using group: 'com.hynnet', name:'json-lib', version:'2.4'or maybe compile 'net.sf.json-lib:json-lib:2.4:jdk8'in gradle to import dependencies, or build our own component with @Data annotation, etc. Frontend is easier to handle.

2. How to handle raw JSON string with \r\n in frontend in JSX?

  • If we use JSON.parse directly on that each string in the String Array, it will have error “Object with keys cannot be a React child”, since after JSON.parse, each string will become Json format with keys and values.

  • If we use JSON.stringify on that

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值