【问题标题】:Read value from string which looks like jSon format从看起来像 json 格式的字符串中读取值
【发布时间】:2018-11-10 02:49:36
【问题描述】:

我有以下格式存储的 cookie 值

{stamp:'HMzWoJn8V4ZkdRN1DduMHLhS3dKiDDr6VoXCjjeuDMO2w6V+n2CcOg==',necessary:true,preferences:true,statistics:true,marketing:false,ver:1}

我需要阅读以下值

necessary
preferences
statistics
marketing

不确定如何正确读取值,假设它是 JSON 格式,我尝试了以下代码

        Cookies.get('CookieConsent')

        //Parse the cookie to Object

        cookieval = Cookies.get('CookieConsent');
        console.log(cookieval);

        console.log("Necessary: " + Boolean(cookieval.necessary));
        console.log("Prefrences: " + Boolean(cookieval.preferences));
        console.log("Statistics: " + Boolean(cookieval.statistics));
        console.log("Marketing: " + Boolean(cookieval.marketing));

但是这段代码总是返回 false。

我使用以下 Jquery 读取 Cookie 值https://cdn.jsdelivr.net/npm/js-cookie@2/src/js.cookie.min.js

【问题讨论】:

    标签: javascript jquery jquery-cookie


    【解决方案1】:

    () 包装这个字符串。然后像 display follow 一样解析

    注意! 但是您需要确保input 字符串(从 cookie 接收)不包含错误代码。比如未知的注入函数。在这种情况下,该函数将在客户端浏览器上执行,并访问私有数据(cookie、localStorage、来自 html-forms 的数据)。

    const input = "{stamp:'HMzWoJn8V4ZkdRN1DduMHLhS3dKiDDr6VoXCjjeuDMO2w6V+n2CcOg==',necessary:true,preferences:true,statistics:true,marketing:false,ver:1}"
    const object = eval("(" + input + ")");
    alert(object.necessary);

    【讨论】:

    • 此方法工作可靠,购买不安全。其他答案中的方法仅适用于简单的输入字符串,但很安全。
    【解决方案2】:

    你没有 JSON 格式 - 你有一些更接近 JS 对象文字表示法的东西,除了它是一个字符串而不是 JS 代码,所以很遗憾不能使用JSON.parse

    如果值没有逗号或冒号,您可以将split字符串通过逗号和reduce转换成一个对象:

    const input = `{stamp:'HMzWoJn8V4ZkdRN1DduMHLhS3dKiDDr6VoXCjjeuDMO2w6V+n2CcOg==',necessary:true,preferences:true,statistics:true,marketing:false,ver:1}`;
    const obj = input
      .slice(1, input.length - 1)
      .split(',')
      .reduce((obj, str) => {
        const [key, val] = str.split(':');
        obj[key] = val;
        return obj;
      }, {});
    console.log(obj);

    eval 是另一种选择,但这是不安全的。

    【讨论】:

    • 运行代码 sn-p - 只需访问 obj.preferences 或您需要的任何属性
    • 感谢 console.log(obj.necessary);
    【解决方案3】:

    如何将字符串按摩成正确的 JSON,将其解析为 JSON 对象,然后使用那里的字段?

    它不太稳定,因为对输入字符串的更改可能会破坏函数,但它是安全的,因为它调用的是 JSON.parse() 而不是 eval()。

       function reformatCookieInput(inputString) {
        inputString = inputString.replace(/'/g, "");  //global strip out single quotes currently wrapping stamp
        inputString = inputString.replace(/,/g, `", "`); //global replace commas with wrapped commas
        inputString = inputString.replace(/:/g, `":"`); //same idea with colons
        inputString = inputString.replace("{", `{"`); //rewrap start of JSON string
        inputString = inputString.replace("}", `"}`); //rewrap end of JSON string
    
        return inputString;
    }
    
    const input = `{stamp:'HMzWoJn8V4ZkdRN1DduMHLhS3dKiDDr6VoXCjjeuDMO2w6V+n2CcOg==',necessary:true,preferences:true,statistics:true,marketing:false,ver:1}`;
    
    const properJSONObject = JSON.parse(reformatCookieInput(input));
    console.log(properJSONObject);

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多