第一种方式:

使用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. 【Hibernate】Hibernate系列7之二级缓存

    二级缓存 7.1.概述 7.2.配置方法

  2. 【leetcode】3Sum

    3Sum Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find a ...

  3. springJDBC实现mysql简单分页

    效果图:

  4. sublime text 3 使用过程总结记录

    自定义的设置: "save_on_focus_lost": true //在文件失去焦点的时候自动保存

  5. MVC文件夹

    应用程序信息: Properties 引用应用程序文件夹: App_Data 文件夹 Content 文件夹 Controllers 文件夹 Models 文件夹 Scripts 文件夹 Views ...

  6. MySQL监控

    http://blog.csdn.net/zreodown/article/details/8158469

  7. git提交报异常,fatal: The remote end hung up unexpectedly

    转自:http://liucanwen.iteye.com/blog/2021601 早上提交代码到 oschina代码库时,报了这个错误: fatal: The remote end hung up ...

  8. POJ3264Balanced Lineup 线段树练手

    题目意思:给定Q(1<=Q<=200000)个数A1,A2,```,AQ,多次求任一区间Ai-Aj中最大数和最小数的差 #include <iostream> #include ...

  9. loj 1004(dp)

    题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=25830‘ 思路:类似与数塔问题,自底向上处理,输入的时候稍微注意一 ...

  10. 电赛菜鸟营培训(四)——STM32F103CB之ADC转换

    一.ADC概念 实现模拟信号转换成数字信号就是这样子= = 二.代码框架 #include "stm32f10x.h" void delay(u32 kk) { while(kk- ...