search

Home  >  Q&A  >  body text

javascript - The data format passed from the backend is like this, how to use it?

Question: In the project, the data format passed from the backend is like this. How do I get the value in BYMONTH?

  INTERVAL=8;BYMONTH=9;BYMONTHDAY=17 
  

Thinking: A simple method I thought of is to parse it into JSON:

  var str = "INTERVAL=8;BYMONTH=9;BYMONTHDAY=17";
  var fiStr = '"' + str.replace(/=/g,'":"').replace(/;/g,'","');
var lastST = '{' + fiStr + '"}';
var Obj = JSON.parse(lastST);
console.log(Obj.BYMONTH)    

Question:
How should I handle this data format?

某草草某草草2748 days ago837

reply all(4)I'll reply

  • 怪我咯

    怪我咯2017-06-30 09:58:44

    var res = {};
    str.split(';').map(function(v){
        var i = v.split('=');
        res[i[0]]=i[1];
    });
    console.log(res['BYMONTH']);

    reply
    0
  • 某草草

    某草草2017-06-30 09:58:44

    "INTERVAL=8;BYMONTH=9;BYMONTHDAY=17".split(";")[2].split("=")[1]

    reply
    0
  • 黄舟

    黄舟2017-06-30 09:58:44

    function getUrlParam(sUrl, sKey) {
        var result,Oparam = {};
        sUrl.replace(/[\;]?(\w+)=(\w+)/g,function($0,$1,$2){
           Oparam[$1]=$2;
        });
        sKey === void 0||sKey==='' ? result=Oparam : result=Oparam[sKey]||'';
        return result;
    }
    getUrlParam("INTERVAL=8;BYMONTH=9;BYMONTHDAY=17","BYMONTH")  //9

    reply
    0
  • 大家讲道理

    大家讲道理2017-06-30 09:58:44

    What I’m more curious about is why the backend doesn’t directly return json format? It has to be processed on the front end.

    reply
    0
  • Cancelreply