Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting string to object with Javascript

I am trying to convert to object this string.

   "JwtBody { user_id: 1, auth_id: 1}"

1 Answers

"JwtBody { user_id: 1, auth_id: 1}" is obviously not a standard json string,So you can try this.

function strToObj(str){
   var obj = {};
   if(str&&typeof str ==='string'){
       var objStr = str.match(/\{(.)+\}/g);
       eval("obj ="+objStr);
   }
   return obj
}
like image 170
sl2782087 Avatar answered Sep 04 '26 14:09

sl2782087