AI写作智能体 自主规划任务,支持联网查询和网页读取,多模态高效创作各类分析报告、商业计划、营销方案、教学内容等。 广告
``` { "id": "some", "title": "some", "author": { "name": "\u95eb\u9759" }, "link": [{ "@attributes": { "href": "http://localhost/", "rel": "some" } }, { "@attributes": { "href": "http://localhost/", "rel": "some" } }, { "@attributes": { "href": "http://localhost/", "rel": "some" } }] } ``` 如上,对于服务器返回的json数据,如何获取Link中第一个@attributes的href值? ``` var req = { ...你的返回对象... }; var first_href_val = req.link[0]['@attributes']; ``` ## 其他(不支持数组) ``` function getJsonValue(obj,name){ var result = null; var value = null; for(var key in obj){ value = obj[key]; if(key == name){ return value; } else { if( typeof value == "object" ){ result = getJsonValue(value,name); } } } return result; } ``` 采用递归方式来查找`josn`中的你需要的`key`. 但是还不支持 数组,你可以自己扩充,思路就是这样! ``` var jsonobj = { "semantic":{"taskId":"8.4.3"},"history":"cn.xxxx.fund"}; var taskId = getJsonValue(jsonobj,"history"); console.log(taskId); ```