下面的是JSON数据?答案是否定的。因为
data
这个key对应的value是个JSON字符串,而不是JSON对象。如果想要取出里面的media_id
的话,使用ret.data.item[0]. media_id
就会报错。基于原理:对象才有点语法,字符串没有点语法。
var ret = {
"code": 1,
"msg": "信息保存成功!",
"data": "{"item":[
{"media_id":"B4hM9KFMHrXAS-eAYn4VOtJxx2_nxHR3LUJx6Jd6NWg",
"content":{"news_item":[
{"title":"测试一下标题","author":"admin","digest":"我是内容哦,哈哈","content":"<p>我是内容哦,哈哈<\\/p><p><mp-miniprogramclass=\"miniprogram_element\" data-miniprogram-appid=\"wxe3a12c133c18dd49\" data-miniprogram-path=\"pages\\/start\\/index\" data-miniprogram-nickname=\"优才用用\" data-miniprogram-avatar=\"http:\\/\\/mmbiz.qpic.cn\\/mmbiz_png\\/YlhWUfxVUdChLGAMFLysrtfG5F4xJl1WkmVpf84RBvic5Qph3RgNxOmuweYkM9vPoxMIeTtibM3LgvZeDPlrttkg\\/640?wx_fmt=png&wxfrom=200\" data-miniprogram-title=\"看下是什么东西\" data-miniprogram-imageurl=\"http:\\/\\/mmbiz.qpic.cn\\/mmbiz_jpg\\/QaHtuC78K7FaZKCbB6uiaJ0k5OvS6sKHLahqWKQBuSInf8fptJ0hPHic1xuglo4R36nX3yEjYZxUlbtI83rKoMvg\\/0?wx_fmt=jpeg\" data-miniprogram-type=\"card\" data-miniprogram-servicetype=\"0\"><\\/mp-miniprogram><\\/p>",
"content_source_url": "",
"thumb_media_id": "B4hM9KFMHrXAS-eAYn4VOu6UZHbVX4xHqVlr2ReaT2c",
"show_cover_pic": 0,
"url": "http:\\/\\/mp.weixin.qq.com\\/s?__biz=MzI0ODgzNDEyMA==&mid=100000009&idx=1&sn=095af3ad3872e854f4fcb00061324d1e&chksm=699bf02e5eec7938039eae18e40ce8b962e4cd1c44e95b3dd9883b2448ccf6ead4fa7d19a288#rd",
"thumb_url": "http:\\/\\/mmbiz.qpic.cn\\/mmbiz_jpg\\/QaHtuC78K7FaZKCbB6uiaJ0k5OvS6sKHL7A8Cjg3qIRCB2GJPl4ruhVL1kbFDibjGxXkefibGURvnwlJWE3iaOaNYQ\\/0?wx_fmt=jpeg",
"need_open_comment": 0,
"only_fans_can_comment": 0
},
{
"title": "测试第二个文章",
"author": "土豪",
"digest": "我是土豪,我有钱,好多好多钱",
"content": "<p>我是土豪,我有钱,好多好多钱<\\/p><p style=\"text-align: center;\"><img class=\"rich_pages js_insertlocalimg\" data-ratio=\"1.2595325953259533\" data-s=\"300,640\" data-src=\"https:\\/\\/mmbiz.qpic.cn\\/mmbiz_jpg\\/QaHtuC78K7GC5JCmByUxrF86mMiaEiaVfDCUHakzsYiaxbDGia6rh4ADnfhnZXwic40NCf17r98Ba1bRE74xnT73o6A\\/640?wx_fmt=jpeg\" data-type=\"jpeg\" data-w=\"813\" style=\"\" \\/><\\/p><p><br \\/><\\/p>",
"content_source_url": "",
"thumb_media_id": "",
"show_cover_pic": 0,
"url": "http:\\/\\/mp.weixin.qq.com\\/s?__biz=MzI0ODgzNDEyMA==&mid=100000009&idx=2&sn=ffe5568e54814ad2f1fda184e8b08851&chksm=699bf02e5eec7938ca05e0d09a90f8d5a7c3ac4b9dd05f988a8dd1fbe80aabe14a0d1fb53216#rd",
"thumb_url": "http:\\/\\/mmbiz.qpic.cn\\/mmbiz_jpg\\/QaHtuC78K7GC5JCmByUxrF86mMiaEiaVfDVVQ4IJ5ls2mibcuNFDAdFfvDCCsxu5lYgHEOVCIbiaZUhwb83S2Q7gUA\\/0?wx_fmt=jpeg",
"need_open_comment": 0,
"only_fans_can_comment": 0
},
{
"title": "分享图片",
"author": "",
"digest": "",
"content": "",
"content_source_url": "",
"thumb_media_id": "",
"show_cover_pic": 0,
"url": "http:\\/\\/mp.weixin.qq.com\\/s?__biz=MzI0ODgzNDEyMA==&mid=100000009&idx=3&sn=5dae90d9f126f3d05f841ccfa7f2c9a4&chksm=699bf02e5eec7938d8097183070ab395c35077be36ea22937c2b48130d1f6806f3e9b656d467#rd",
"thumb_url": "http:\\/\\/mmbiz.qpic.cn\\/mmbiz_jpg\\/QaHtuC78K7GC5JCmByUxrF86mMiaEiaVfD4bfkP4m6qicgc4OmQeErRh2a5CHgZQEqdxdfod2VMQCYgaj81D1IrUA\\/0",
"need_open_comment": 0,
"only_fans_can_comment": 0
},
{
"title": "第三条标题",
"author": "我是标",
"digest": "我是第三条的内容",
"content": "<p>我是第三条的内容<\\/p>",
"content_source_url": "",
"thumb_media_id": "B4hM9KFMHrXAS-eAYn4VOiKfrZeH5-Oa8GVCJoj73Lg",
"show_cover_pic": 0,
"url": "http:\\/\\/mp.weixin.qq.com\\/s?__biz=MzI0ODgzNDEyMA==&mid=100000009&idx=4&sn=eba470503fa2dbb7bd63efe2cc3da09f&chksm=699bf02e5eec7938ba7d514b505c4b7a2e94a58da035efad8018787291233104798af01372fb#rd",
"thumb_url": "http:\\/\\/mmbiz.qpic.cn\\/mmbiz_jpg\\/QaHtuC78K7GC5JCmByUxrF86mMiaEiaVfDYDF60nJ5OiaOCyM84KDSvy9JPaYoWkfugicaw5Cop2olRF9WEVyVhK1A\\/0?wx_fmt=jpeg",
"need_open_comment": 0,
"only_fans_can_comment": 0
}
],
"create_time": 1572340301,
"update_time": 1572340983
},
"update_time": 1572340983
},
{
"media_id": "B4hM9KFMHrXAS-eAYn4VOuZP09Q-vn7Q4-CK8B_gBdI",
"content": {
"news_item": [
{
"title": "测试一下标题",
"author": "admin",
"digest": "我是内容哦,哈哈",
"content": "<p>我是内容哦,哈哈<\\/p><p><mp-miniprogram class=\"miniprogram_element\" data-miniprogram-appid=\"wxe3a12c133c18dd49\" data-miniprogram-path=\"pages\\/start\\/index\" data-miniprogram-nickname=\"优才用用\" data-miniprogram-avatar=\"http:\\/\\/mmbiz.qpic.cn\\/mmbiz_png\\/YlhWUfxVUdChLGAMFLysrtfG5F4xJl1WkmVpf84RBvic5Qph3RgNxOmuweYkM9vPoxMIeTtibM3LgvZeDPlrttkg\\/640?wx_fmt=png&wxfrom=200\" data-miniprogram-title=\"看下是什么东西\" data-miniprogram-imageurl=\"http:\\/\\/mmbiz.qpic.cn\\/mmbiz_jpg\\/QaHtuC78K7FaZKCbB6uiaJ0k5OvS6sKHLahqWKQBuSInf8fptJ0hPHic1xuglo4R36nX3yEjYZxUlbtI83rKoMvg\\/0?wx_fmt=jpeg\" data-miniprogram-type=\"card\" data-miniprogram-servicetype=\"0\"><\\/mp-miniprogram><\\/p>",
"content_source_url": "",
"thumb_media_id": "B4hM9KFMHrXAS-eAYn4VOu6UZHbVX4xHqVlr2ReaT2c",
"show_cover_pic": 0,
"url": "http:\\/\\/mp.weixin.qq.com\\/s?__biz=MzI0ODgzNDEyMA==&mid=100000002&idx=1&sn=7cbd7795a34086251acf7652e512fd34&chksm=699bf0255eec7933bde1ca58a4122f2444b48962af03bff50a9991b6fdd9c47c59be0e497a8e#rd",
"thumb_url": "http:\\/\\/mmbiz.qpic.cn\\/mmbiz_jpg\\/QaHtuC78K7FaZKCbB6uiaJ0k5OvS6sKHL7A8Cjg3qIRCB2GJPl4ruhVL1kbFDibjGxXkefibGURvnwlJWE3iaOaNYQ\\/0?wx_fmt=jpeg",
"need_open_comment": 0,
"only_fans_can_comment": 0
}
],
"create_time": 1571910035,
"update_time": 1571910061
},
"update_time": 1571910061
}
],
"total_count": 2,
"item_count": 2
}"}
去掉data对应的双引号之后
解决办法:先使用JSON.parse(json字符串)
,将data对应的JSON字符串转成JSON对象(jsonA
),然后再使用jsonA.item[0].media_id
取出media_id
var jsonA = JSON.parse(ret.data);
console.log("media_id数据为" + jsonA.item[0].media_id)
总结:
一、判别:value中如果有嵌套数据,那么value不能是字符串。上面例子中,data
这个key对应的value是个JSON字符串,而不是JSON对象。
二、解析:使用JSON.parse(json字符串)
将json字符串解析成json数据