第一种方式:

使用js函数eval();

testJson=eval(testJson);是错误的转换方式。

正确的转换方式需要加(): testJson = eval("(" + testJson + ")");

eval()的速度非常快,但是他可以编译以及执行任何javaScript程序,所以会存在安全问题。在使用eval()。来源必须是值得信赖的。需要使用更安全的json解析器。在服务器不严格的编码在json或者如果不严格验证的输入,就有可能提供无效的json或者载有危险的脚本,在eval()中执行脚本,释放恶意代码。

js代码:

  1. function ConvertToJsonForJs() {
  2. //var testJson = "{ name: '小强', age: 16 }";(支持)
  3. //var testJson = "{ 'name': '小强', 'age': 16 }";(支持)
  4. var testJson = '{ "name": "小强", "age": 16 }';
  5. //testJson=eval(testJson);//错误的转换方式
  6. testJson = eval("(" + testJson + ")");
  7. alert(testJson.name);
  8. }

第二种方式使用jquery.parseJSON()方法对json的格式要求比较高,必须符合json格式

jquery.parseJSON()

js:代码

  1. function ConvertToJsonForJq() {
  2. var testJson = '{ "name": "小强", "age": 16 }';
  3. //不知道
  4. //'{ name: "小强", age: 16 }' (name 没有使用双引号包裹)
  5. //"{ 'name': "小强", 'age': 16 }"(name使用单引号)
  6. testJson = $.parseJSON(testJson);
  7. alert(testJson.name);
  8. }

(转)js:字符串(string)转json的更多相关文章

  1. js:字符串(string)转json

    第一种方式: 使用js函数eval(); testJson=eval(testJson);是错误的转换方式. 正确的转换方式需要加(): testJson = eval("(" + ...

  2. 使用js进行string和json之间转换的方法

    在数据传输过种中,json是以文本,即字符串的形式传递,字符串形似Json对象: var str1 = '{ "name": "Amy", "sex& ...

  3. JS于string 和 json互转对象

    一.json开启string JSON.stringify(jsonObj) 两.string开启json eval(string) 版权声明:本文博主原创文章.博客,未经同意不得转载.

  4. 字符串(string)转json

    第一种方式: 使用js函数eval(); testJson=eval(testJson);是错误的转换方式. 正确的转换方式需要加(): testJson = eval("(" + ...

  5. js字符串String提取方法比较

    JavaScript: Slice, Substring, or Substr的选择! 在JavaScript中,字符串主要通过以下String方法之一提取: // slice // syntax: ...

  6. js字符串String常用方法

    1.   charAt()         返回指定位置的字符. str.charAt(index) index 为必须参数,类型为number(0到str.length-1之间,否则该方法返回 空串 ...

  7. jquery:字符串(string)转json

    第一种方式: 使用js函数eval(); testJson=eval(testJson);是错误的转换方式. 正确的转换方式需要加(): testJson = eval("(" + ...

  8. js中string和json的相互转换

    1.将string转成json var json={}; var myorderno=$("#myorderno").val(); json.myorderno=myorderno ...

  9. JS 字符串 String对象

    charAt(index) 返回指定索引位置的字符 charCodeAt() 返回指定索引位置字符的 Unicode 值 indexOf(searchString, startIndex) 返回子字符 ...

随机推荐

  1. spring事物传播属性

    PROPAGATION_REQUIRED Support a current transaction; create a new one if none exists.  支持一个当前事务;如果不存在 ...

  2. Cocos2d 中的Sprite大小调整问题

    以前用UIImageView,比如  UIImageView *view = [[UIImageViewalloc] initWithImage:[UIImageimageNamed:@"b ...

  3. iOS UITableView 的beginUpdates和endUpdates

    在官方文档中是这样介绍beginUpdates的 Call this method if you want subsequent insertions, deletion, and selection ...

  4. liunx下安装MYSQL时需要安装的相关软件的作用

    2013年11月16日 14:18:39 This installs the package for MySQL server (mysql-community-server) and also pa ...

  5. 转mysql复制主从集群搭建

    最近搭了个主从复制,中间出了点小问题,排查搞定,记录下来 1环境:虚拟机:OS:centos6.5Linux host2 2.6.32-431.el6.x86_64 #1 SMP Fri Nov 22 ...

  6. Java for LeetCode 053 Maximum Subarray

    Find the contiguous subarray within an array (containing at least one number) which has the largest ...

  7. iOS通过设置info.plist参数使用iTunes导入导出Documents目录下的文件

    参考网址: http://my.oschina.net/hmj/blog/112592 http://www.cnblogs.com/taintain1984/archive/2013/05/27/3 ...

  8. sublime text3侧边栏主题不生效问题解决

    sublime text3主题插件: Seti_UI 插件安装: 在线安装:需要FQ window: ctrl+shift+p 找install package:之后搜索 Seti_UI 安装完成后需 ...

  9. static_cast, dynamic_cast, const_cast

    http://www.cnblogs.com/chio/archive/2007/07/18/822389.html 首先回顾一下C++类型转换: C++类型转换分为:隐式类型转换和显式类型转换 第1 ...

  10. Instruments_Automation使用入门

    Instruments 是应用程序用来动态跟踪和分析 Mac OS X 和 iOS 代码的实用工具. 这是一个灵活而强大的工具,它让你可以跟踪一个或多个进程,并检查收集的数据. 这样,Instrume ...