undefinedfix
Sign in

JSON, which can't get the key value after local storage

lior edited in Thu, 26 Jan 2023
Because it's mixed development, I can only put the data given by IOS on the (APP installed on mobile phone) page, but it can't be printed

The given value is like this. At this time, the key value is failed

587927ecd8c2725507c1e67a91fcae8.jpg

I do the following processing: store and save the current page:

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

When I need to use it


    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 }
        }
      })
    }

Only in mode 2, the following data will be displayed on the page (as before)

d4f0b951b1328fbb86697ea58c00e36.jpg

Anyway, I can't get its key value. What should I do?

4 Replies
Beelzeko
commented on Thu, 26 Jan 2023

First of all, I would like to thank Linong and hfhan for their answers, which gave me a lot of ideas. Thank you~~

My problem is that the data is wrong. You have noticed that my data does not have the JSON string that IOS gave me. This is no problem. However, because there was a lot of data transferred at that time, I wanted to transfer it at one time. Then, after the userinfo (JSON object) was changed into a JSON string, it was assigned to the key of another JSON object. Finally, the final JSON object was serialized (stringed) and sent to me. It's OK to put it on the page, but JSON.parse It's a mistake, OA is the final data for me. Finally, the parsing failed. Naturally, I couldn't get other key values. I couldn't catch any errors on the app page, which was very uncomfortable~~~

Summary: the key value of a JSON object cannot be a serialized (stringify) JSON object, otherwise the parsing will report an error!

sbarb
commented on Thu, 26 Jan 2023
  1. First output params directly to see what it is
  2. Type of params to see whether it's a string or an object. I think string is in the majorityIf it is an object, it will be output directly JSON.stringify (params) take a look. Make sense params.orderID It should be valuable.If it's a string, it needs to be JSON.parse (params)
  3. Then continue with typeof JSON.parse (params), continue to set the baby.If there is no mistake, go back to step 2Four steps to report a mistake
  4. JSON.parse (string) you can try Eval when you can't handle it

Let's prove that typeof works well

aslan
commented on Fri, 27 Jan 2023

Localstorage access is all for string operation, so you need to convert it into string before saving, and then retrieve it JSON.parse . Instead of saving it directly, take it out JSON.parse ( JSON.stringify (params))

user846566
commented on Fri, 27 Jan 2023

In fact, two friends upstairs have already solved the problem for you. It's just that there is no unified integration, and the reasons for each step are analyzed

Take these three lines of code

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

const params: string | null = localStorage.getItem('params')

Look at three lines of code, localStorage.setItem ('params', params) is a literal bug. Whether it is caused by this sentence or not, we have solved it first.

if(typeof params==='object'){
    localStorage.setItem('params', JSON.stringify(params))
}else if(typeof params === 'string'){
    localStorage.setItem('params', params)
}else{
    alert('数据格式是:'+typeof params)
}

I guess the problem should be that the transformation operation is not fault-tolerant.

if(param){
    let p = '';
    try{
        p = JSON.parse(param)
    }catch(e){
        // debugger 看看报的什么错误, 正式环境注释掉alert
        // 不出意外的话,可能和你之前存储的脏数据有关系。
        alert(e.toString())
        p = {};
    }
}

in addition

const { token } = p.userInfo.token

I don't know whether this definition grammar is correct or not. I will report an error in my test.