<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>JSON.parse()</title>
<script type="text/javascript">
//演示样例1:此演示样例使用 JSON.parse 将 JSON 字符串转换为对象
var jsontext = '{"firstname":"Jesper","surname":"Aaberg","phone":["555-0100","555-0120"]}';//JSON 字符串
var contact = JSON.parse(jsontext);
document.write(contact.surname + ", " + contact.firstname + ", "+ contact.phone); //演示样例2:和实例1是一样的效果
var jsontext2 = {"firstname":"Jesper","surname":"Aaberg","phone":["555-0100","555-0120"]};//JSON 对象
//var contact2 = JSON.parse(jsontext2);
document.write("<br /><br />"+jsontext2.surname + ", " + jsontext2.firstname + ", "+ jsontext2.phone);
</script>
</head>
<body>
</body>
</html>

输出:

Aaberg, Jesper, 555-0100,555-0120

Aaberg, Jesper, 555-0100,555-0120

************************************************************************************

前端页面接收JSON对象的实例:

<script>
sendRecord('1');
function sendRecord(record){
var req = {
user_id:<?php echo $userId;?>,
record:record,
}
$.ajax({
url: "./a.php",
type:"post",
data:req,
dataType:"JSON", //返回数据格式为JSON对象
success: function(res){
alert(Object.prototype.toString.apply(res));//alert输出[object Object],不用JSON.parse()解析
if(res.result==1){
alert('11');
}else if(res.result==2){
alert('22');
}else if(res.result==3){
alert('33');
}
},
error: function(){
alert('error000');
console.log(this);
}
});
}
</script>

a.php

<?php
$record = $_POST['record']; if ($record==1) {
$json['result'] = 1;
}elseif($record==2){
$json['result'] = 2;
}elseif($record==3){
$json['result'] = 3;
} $json = json_encode($json);
echo $json; //{"result":1}
?>

***************************************************************************

<script type="text/javascript" src="./jquery.min.js"></script>
<script>
sendRecord('2');
function sendRecord(record){
var req = {
record:record,
}
$.ajax({
url: "./b.php",
type:"post",
data:req,
dataType:"JSON", //返回数据格式为JSON对象
success: function(res){
//加上dataType:"JSON",alert输出[object Array],不须要JSON.parse()解析
//不加dataType:"JSON",alert输出[object String],须要JSON.parse()解析
alert(Object.prototype.toString.apply(res));
//var res2 = JSON.parse(res);
alert(res);
},
error: function(){
alert('error000');
console.log(this);
}
});
}
</script>

b.php

<?

php
$record = $_POST['record'];
if ($record==1) {
$json = array('a','aa');
}elseif($record==2){
$json = array('b','bb');
}elseif($record==3){
$json = array('c','cc');
}
$json = json_encode($json);
echo $json; //["b","bb"]
? >

总结:假设后台是通过echo json_encode();输出json字符串的话,并且前台使用了dataType:"JSON", 那么则不须要JSON.parse();把json字符串转换成对象或数组。

JSON对象和JSON字符串以及JSON.parse 函数的使用的更多相关文章

  1. json字符串转换成json对象,json对象转换成字符串,值转换成字符串,字符串转成值

    一.json相关概念 json,全称为javascript object notation,是一种轻量级的数据交互格式.采用完全独立于语言的文本格式,是一种理想的数据交换格式. 同时,json是jav ...

  2. 前台 JSON对象转换成字符串 相互转换 的几种方式

    在最近的工作中,使用到JSON进行数据的传递,特别是从前端传递到后台,前台可以直接采用ajax的data函数,按json格式传递,后台Request即可,但有的时候,需要传递多个参数,后台使用requ ...

  3. JS json对象(Object)和字符串(String)互转方法

    [JS json对象(Object)和字符串(String)互转方法] 参考:https://blog.csdn.net/wenqianla2550/article/details/78232706 ...

  4. js 将json对象转成字符串

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  5. 关于JSON对象,以及联合数组,eval函数的使用参考

    关于JSON对象,以及联合数组,eval函数的使用参考 var json="{persons:[{name:'Zhangsan',sex:'male'},{name:'Lisi',sex:' ...

  6. 获取Json对象的长度以及判断json对象是否为空

    (如有错敬请指点,以下是我工作中遇到并且解决的问题) = = = = = = = = = = = = = = = =  获取Json对象的长度  = = = = = = = = = = = = = = ...

  7. JSON对象获取指定元素以及JSON.parse() 与 JSON.stringify() 的区别

    利用 JSON.parse(param) 实现 例: var param = { "name" : "张三", "text" : { &qu ...

  8. vue axios get请求参数为json对象 而非字符串形式

    axios get请求方式 传递给后台的参数都是字符串下形式,无法传递json对象 或数组对象等    post请求方式则可以实现,   但若后台接口要求必须用get方式传递对象给后台,需要装插件,实 ...

  9. JSON对象转换成字符串【JSON2.JS】

    下载地址 https://github.com/douglascrockford/JSON-js JSON.JS和JSON2.JS的区别 JSON.JS使用的方法名称不同,用的是toJSONStrin ...

  10. LocalStorage存储JSON对象的问题

    LocalStorage存储JSON对象的问题   localStorage - 没有时间限制的数据存储 1 var arr=[1,2,3]; 2 localStorage.setItem(" ...

随机推荐

  1. jdbc preparestatement和preparestatement区别

    1.preparestatement预编译,预编译指的是DB的编译器,会对此sql语句提前编译.然后将预编译的结果缓存到数据库中,下次执行时替换参数直接执行编译过的语句. 记住:数据库也是有编译器的, ...

  2. 【资料】wod烟草

    注意: 1. 所有效果持续时间是整个地城 2. 某几样菸草在使用 烟雾的祝福 的时候效果只有LV1 (技能 -25), 表示该物品设计上主要是自己使用而非加给团队. SL = 技能等级 HL = 英雄 ...

  3. 使用Struts2服务端与android交互

    转自:http://www.cnblogs.com/android-html5/archive/2011/09/25/2534107.html android--使用Struts2服务端与androi ...

  4. [译林军] 译~CrossBridge 简介

    本文由 9ria 社区译林军翻译,转载请注明出处.加入译林军 :http://bbs.9ria.com/thread-286920-1-1.html CrossBridge 是  Adobe  Fla ...

  5. dedecms织梦 v5.5 两处跨站漏洞

    漏洞版本: dedecms织梦5.5 漏洞描述: 北洋贱队(http://bbs.seceye.org)首发 demo1:http://www.dedecms.com/plus/search.php? ...

  6. IOS中的XML解析之DOM和SAX

    一.介绍 dom是w3c指定的一套规范标准,核心是按树形结构处理数据,dom解析器读入xml文件并在内存中建立一个结构一模一样的“树”,这树各节点和xml各标记对应,通过操纵此“树”来处理xml中的文 ...

  7. C# 怎么显示中文格式的星期几

    1.DateTime.Now.ToString("dddd",new System.Globalization.CultureInfo("zh-cn")); 2 ...

  8. python爬虫模拟登陆

    python爬虫模拟登陆 学习了:https://www.cnblogs.com/chenxiaohan/p/7654667.html  用的这个 学习了:https://www.cnblogs.co ...

  9. [Algorithm] Reverse array of Chars by word

    For example we have: ["p", "r", "e", "f", "e", &qu ...

  10. [Angular-Scaled web] 3. Basic State with ui-router

    1. Install ui-route, include js file in html and add dependence in js file. bower install angular-ui ...