gpt4 book ai didi

javascript - 多个返回键json

转载 作者:行者123 更新时间:2023-11-30 08:36:48 25 4
gpt4 key购买 nike

我有一个运行 k2 的 joomla 站点,并通过在字符串末尾添加 format=json 从中提取信息:https://www.example.com/posts?format=json输出是这样的:

{
site: {
url: "https://www.example.com",
name: "mySite"
},
category: {
id: "67",
name: "Gauteng",
alias: "gauteng",
link: "/tna/provincial/gauteng.html",
parent: "66",
extraFieldsGroup: "0",
image: null,
ordering: "1",
events: {
K2CategoryDisplay: ""
},
chidlren: []
},
items: [{
id="1",
title="The Title",
body="body content here..."
},
{
id="2",
title="The Title",
body="body content here..."
}
}

现在我正在为此创建一个服务,只想访问“项目”。

.factory('Posts', function($http) {

var posts = [];

return {
getPosts: function(){
return $http.get("https://www.example.com/posts?format=json").then(function(response){
posts = response;
return posts;
});
},
getPost: function(index){
return posts[index];
}
}

});

它似乎并没有起作用。有什么方法可以通过调用访问“项目”吗?

最佳答案

你的帖子语法应该是posts = response.data.items

return $http.get("https://www.example.com/posts?format=json").then(function(response){
posts = response.data.items; //items here
return posts;
});

关于javascript - 多个返回键json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30594739/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com