与 JSON 相关的一些操做 (项目中用到一部分,后续逐渐完善)

1.JSON 打印json

  console.log(JSON.stringify(data, null, 4));spa

2.JSON 格式判断prototype

  var isjson = typeof(obj) == "object" && Object.prototype.toString.call(obj).toLowerCase() == "[object object]" && !obj.length;blog

3.JSON 转义string

  (当咱们在多行文本框中对JSON数据进行操做以后,数据会进行转义,会带有 \\|\"|\n|\r|\t 这一类字符console

  var toObj = JSON.parse(str.replace(/(\\|\"|\n|\r|\t)/g, "\$1"));object

4.JSON 内部数据是否正确date

  try-catch抛异常:im

  if(typeof(vm.tobeupdatemenuVal) == "object"){call

    var dataJSON = {
      "content": vm.tobeupdatemenuVal

    };
  }else{
    console.log(121)
    try{
      var toObjVal = JSON.parse(vm.tobeupdatemenuVal.replace(/(\\|\"|\n|\r|\t)/g, "\$1"));
    }catch(e){
      layer.msg("数据格式错误!")
      return;
    }
    var dataJSON = {
      "content": toObjVal
    };
  }

  

  

相关文章
相关标签/搜索