【发布时间】:2019-05-10 02:02:52
【问题描述】:
我在互联网上搜索了答案,我发现JSON.stringify() 不能转换例如函数,只能转换纯数据。
但是这个答案不能满足我的需要。
由于AsyncStorage 的工作方式,我需要将我通过fetch 请求的json 保存在一个字符串中
所以我将jsonContent 保存为字符串化版本(jsonContent 是获取的json)
var temp = JSON.stringify(jsonContent);
稍后我需要重新调用它,因此我将其返回为 json 格式,但它不接受将输出作为 json 输出。
var output = JSON.parse(temp);
我要获取的json 是:
{
"content": [{
"name": "this",
"desc": "is",
"explanation": "just a",
"time": "test",
"class": "with one",
"image": "item",
"id": "0",
"l_name": "testing"
}]
}
jsonContent的获取版本(这个可以用,pre-stringify&parse):
Object {
"content": Array [
Object {
"class": "with one",
"desc": "is",
"explanation": "just a",
"id": "0",
"image": "item",
"l_name": "testing",
"name": "this",
"time": "test",
},
],
}
jsonContent 的获取版本(这个不可用,post-stringify&parse):
Object {
"content": Array [
Object {
"class": "with one",
"desc": "is",
"explanation": "just a",
"id": "0",
"image": "item",
"l_name": "testing",
"name": "this",
"time": "test",
},
],
}
如何修复我的 json 以便我可以使用 JSON.stringify(JSON.parse(jsonContent))
如果您需要更多信息,请询问。 感谢您的宝贵时间。
【问题讨论】:
-
它们在用法上是相同的,但是它们是两个不同的对象,具有完全相同的属性,并且相等运算符(==,===)只有在它们是同一个对象时才会返回true。不要将 console.log 显示的内容与您的实际对象混淆。
标签: json parsing react-native stringify asyncstorage