仅获取存储在localStorage中的项目的值

时间:2018-07-25 12:54:13

标签: javascript reactjs

我想从我保存在localStorage中的值中获取值,但是我也要获取存储的密钥名称。

let sortedFirstChars = firstChars.sorted()

console.log返回类似localStorage.setItem('token', tokenJson); console.log(JSON.parse(localStorage.getItem('token'))); 的内容,但我只想冒号后的内容没有引号。 我不知道它是否清楚,但是我需要令牌,仅需要令牌的值即可进行提取以进行API调用。

我想要的是,当我发出请求时,答案是这样的:

{"token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjo0LCJ1c2VybmFtZSI6InJpY2FyZG8uY2Fycm9sYUBnbWFpbC5jb20iLCJleHAiOjE1MzMxMjc1ODksImVtYWlsIjoicmljYXJkby5jYXJyb2xhQGdtYWlsLmNvbSIsIm9yaWdfaWF0IjoxNTMyNTIyNzg5fQ.nYijM4xxFH_Cuoahi-eximAQcQeQ3v1bDRy09ltgDdA"}

不是这样

JWT eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjo0LCJ1c2VybmFtZSI6InJpY2FyZG8uY2Fycm9sYUBnbWFpbC5jb20iLCJleHAiOjE1MzMxMjc1ODMsImVtYWlsIjoicmljYXJkby5jYXJyb2xhQGdtYWlsLmNvbSIsIm9yaWdfaWF0IjoxNTMyNTIyNzgzfQ.NMTLQCalo9UMMtJXUq7ynzpUbL3m3rjumMw9unoZhtc

3 个答案:

答案 0 :(得分:0)

然后只保存令牌的值而不是对象。

var tokenJson = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjo0LCJ1c2VybmFtZSI6InJpY2FyZG8uY2Fycm9sYUBnbWFpbC5jb20iLCJleHAiOjE1MzMxMjc1ODksImVtYWlsIjoicmljYXJkby5jYXJyb2xhQGdtYWlsLmNvbSIsIm9yaWdfaWF0IjoxNTMyNTIyNzg5fQ.nYijM4xxFH_Cuoahi-eximAQcQeQ3v1bDRy09ltgDdA";

localStorage.setItem('token', tokenJson);
console.log(localStorage.getItem('token'));

// eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjo0LCJ1c2VybmFtZSI6InJpY2FyZG8uY2Fycm9sYUBnbWFpbC5jb20iLCJleHAiOjE1MzMxMjc1ODksImVtYWlsIjoicmljYXJkby5jYXJyb2xhQGdtYWlsLmNvbSIsIm9yaWdfaWF0IjoxNTMyNTIyNzg5fQ.nYijM4xxFH_Cuoahi-eximAQcQeQ3v1bDRy09ltgDdA

答案 1 :(得分:0)

尝试直接使用localStorage.getItem()方法

示例

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

        Bitmap image = (Bitmap)Bitmap.FromFile(@"StandardImage\\lena.png");
        Bitmap conv = new Bitmap(image.Width, image.Height, image.PixelFormat);

        ComplexImage cImage = ComplexImage.FromBitmap(image);
        cImage.ForwardFourierTransform();

        ComplexImage cKernel = ComplexImage.FromBitmap(image);
        cImage.ForwardFourierTransform();

        ComplexImage convOut = ComplexImage.FromBitmap(conv);
        convOut.ForwardFourierTransform();

        for (int y = 0; y < cImage.Height; y++)
        {
            for (int x = 0; x < cImage.Width; x++)
            {
                convOut.Data[x, y] = cImage.Data[x, y] * cKernel.Data[x, y];
            }
        }

        convOut.BackwardFourierTransform();

        Bitmap bbbb = convOut.ToBitmap();

        pictureBox1.Image = bbbb;

    }
}

答案 2 :(得分:0)

通过将.token添加到我的getItem中,我只能访问本地存储的内容。感谢帮助的人!

(JSON.parse(localStorage.getItem('token')).token)