Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
398 views
in Technique[技术] by (71.8m points)

json,本地存储后再取出来拿不到键值

因为是混合开发,ios那边给的数据,我只能把数据放到(手机安装的app)页面来看,打印不出来

给的值大概是这样子,此时由键取值是失败的

587927ecd8c2725507c1e67a91fcae8.jpg

我做了如下处理,存储和当前页面保存:

this.paramsInfo = params
localStorage.setItem('params', params)

我要用的时候


    const params: string | null = localStorage.getItem('params')
    if (params) {
      // 方式1
      // const p = JSON.parse(params)
      // this.viewData = p // 页面显示不出来
      // this.orderInfo = p.orderID
      
      // 方式2
      const p = JSON.parse(JSON.stringify(params))
      this.viewData = p // 页面呈现一样的数据
      this.orderInfo = p.orderID // 依旧无法获取

      const { token } = p.userInfo
      this.$https.orderDetails.getDetails({ token, id: p.orderID }).then((res: fetchResponse) => {
        if (res.flag === 1) {
          this.orderInfo = { ...res.data }
        }
      })
    }

仅在方式2时在页面呈现如下如数据(和前面一样)

d4f0b951b1328fbb86697ea58c00e36.jpg

无论如何,我都取不到它的键值,我该如何做?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

首先感谢 linong与hfhan的回答,给了我很多思路,谢谢~~

我的问题是数据错了,大家注意到我的数据没有,ios给我的JSON字符串,这个没问题。
但是因为当时传的数据比较多,想一次性传过来,然后userInfo(json对象)被变为了json字符串后再赋值给另一个JSON对象的键,最后再把那个最终的json对象序列化(字符串化)后传给我,这样放在页面没问题,但是JSON.parse就会报错,大概意思如下:
9fb63d2f5d39740476084cdcea5347b.png
oa是最终给我的数据。最后解析失败了,自然其它键值就获取不到了,app页面我又捕获不到错误,很难受~~~

总结: JSON对象的键值不能是被序列化(stringify)的JSON对象,否则解析时报错!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...