Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 从json数组获取数据_Javascript_Jquery_Json - Fatal编程技术网

Javascript 从json数组获取数据

Javascript 从json数组获取数据,javascript,jquery,json,Javascript,Jquery,Json,请查看此URL:“”。 这是我网站帖子的json数组。 我想将此数组的每个成员存储在posts数组中。 最后,在posts数组中,将以json格式显示我网站的所有帖子。 在代码的末尾,我尝试获取每个帖子的ID。 我的帖子ID是:34 32 20 24 但它显示:24 请帮帮我,谢谢 $( function(){ //api start var URL = "http://www.zaferteam.com/wp-json/posts/"; //for storing feteched pos

请查看此URL:“”。 这是我网站帖子的json数组。 我想将此数组的每个成员存储在posts数组中。 最后,在posts数组中,将以json格式显示我网站的所有帖子。 在代码的末尾,我尝试获取每个帖子的ID。 我的帖子ID是:34 32 20 24 但它显示:24 请帮帮我,谢谢

$(
function(){ 
//api start 
var URL = "http://www.zaferteam.com/wp-json/posts/";
//for storing feteched posts
var FetchedPost = {
    //----Start wordpress fields----
    ID:"",
    title:"",
    status:"",
    type:"",
    author:{
        ID:"",
        username:"",
        name:"",
        first_name:"",
        last_name:"",
        nickname:"",
        slug:"",
        URL:"",
        avatar:"",
        description:"",
        registered:"",
        meta:{
        linkss:{self:"", 
            archives:""
                }
            }
        },
    content: "",
    parent: "",
    links: "",
    date: "",
    modified: "",
    format: "",
    slug: "",
    guid: "",
    excerpt: "",
    menu_order: "",
    comment_status: "",
    ping_status: "",
    sticky: "",
    date_tz: "",
    date_gmt: "",
    modified_tz: "",
    modified_gmt: "",
    meta:{
        linkss:{self: "", author: ""}
        },
    featured_image:{
        ID: "", title: "", status: "", guid:""
        },
    terms:{
        category:[
                {
                ID:"",
                name:"",
                slug:"",
                description: "",
                taxonomy: "",
                parent: "",
                count: "",
                links: "",
                meta:{
                    linkss:{collection: ""}
                }
            }
        ]
    }
}
//----End wordpress fields----

//methods for fetching data from a post . [input parameter is a post]
var methods ={
//get ID of the post
    ID:function(post){
        FetchedPost.ID = post.ID;
    },
//get title of the post 
    title:function(post){
        FetchedPost.title = post.title;
    },
//get ID of the author ID   
    authorID:function(post){
        FetchedPost.author.ID = post.author.ID;
    },

//get name of the author name   
    authorName:function(post){
        FetchedPost.author.name = post.author.name;
    },
//get username of the author username   
    authorUsername:function(post){
        FetchedPost.author.username = post.author.username;
    },  
//get title of the content  
    content:function(post){
        FetchedPost.content = post.content;
    },
//get links 
    links:function(post){
        FetchedPost.links = post.links;
    },
//get the featured_image guid (featured_image links) of the post
    featuredImageGuid:function(post){

        //alert(typeof post.featured_image.guid);
        if(post.featured_image != null){
        FetchedPost.featured_image.guid = post.featured_image.guid;
        }
        else{
            FetchedPost.featured_image.guid = "#";
            }
        //alert(typeof milad);
    }   
}

function wpMain(post){
    methods.ID(post);
    methods.title(post);
    methods.authorID(post);
    methods.authorName(post);
    methods.authorUsername(post);
    methods.content(post);
    methods.links(post);
    methods.featuredImageGuid(post);
    }  

//fetch by ajax
    $.ajax({
    url: URL,
    success: function(data, status) {
        var localData = JSON.stringify(data);
        window.localStorage.setItem('WPpost', localData);
    },
    error: function() {
        //handle the error
    }
    });
var localData = JSON.parse(window.localStorage.getItem('WPpost'));
        var Length = localData.length;
        var posts = new Array();
        $.each(localData,function(index,value){
            wpMain(value);
            posts.push(FetchedPost);
            });

        $.each(posts,function(index,value){
            alert(value.ID);
            }); 
    });

请尝试以下方法:

创建新函数
函数readLocalStorageData()
,如下所示:

function readLocalStorageData()
{
    var localData = JSON.parse(window.localStorage.getItem('WPpost'));
    var Length = localData.length;
    var posts = new Array();
    $.each(localData,function(index,value){
        wpMain(value);
        posts.push(FetchedPost);
    });

    $.each(posts,function(index,value){
        alert(value.ID);
    }); 
}
//fetch by ajax
        $.ajax({
        url: URL,
        success: function(data, status) {
            var localData = JSON.stringify(data);
            window.localStorage.setItem('WPpost', localData);

            readLocalStorageData(); //read the locally stored data
        },
        error: function() {
            //handle the error
        }
    });
现在,按如下方式调用上述函数以获得ajax
success
处理程序:

function readLocalStorageData()
{
    var localData = JSON.parse(window.localStorage.getItem('WPpost'));
    var Length = localData.length;
    var posts = new Array();
    $.each(localData,function(index,value){
        wpMain(value);
        posts.push(FetchedPost);
    });

    $.each(posts,function(index,value){
        alert(value.ID);
    }); 
}
//fetch by ajax
        $.ajax({
        url: URL,
        success: function(data, status) {
            var localData = JSON.stringify(data);
            window.localStorage.setItem('WPpost', localData);

            readLocalStorageData(); //read the locally stored data
        },
        error: function() {
            //handle the error
        }
    });

这适用于本地json文件:

添加了测试变量

var URL = "my.json";
var test;
//for storing feteched posts
var FetchedPost = {
    //----Start wordpress fields----
    ID: "",
    title: "",
    status: "",
    type: "",
    author: {
        ID: "",
        username: "",
        name: "",
        first_name: "",
        last_name: "",
        nickname: "",
        slug: "",
        URL: "",
        avatar: "",
        description: "",
        registered: "",
        meta: {
            linkss: {self: "",
                archives: ""
            }
        }
    },
    content: "",
    parent: "",
    links: "",
    date: "",
    modified: "",
    format: "",
    slug: "",
    guid: "",
    excerpt: "",
    menu_order: "",
    comment_status: "",
    ping_status: "",
    sticky: "",
    date_tz: "",
    date_gmt: "",
    modified_tz: "",
    modified_gmt: "",
    meta: {
        linkss: {self: "", author: ""}
    },
    featured_image: {
        ID: "", title: "", status: "", guid: ""
    },
    terms: {
        category: [
            {
                ID: "",
                name: "",
                slug: "",
                description: "",
                taxonomy: "",
                parent: "",
                count: "",
                links: "",
                meta: {
                    linkss: {collection: ""}
                }
            }
        ]
    }
}
//----End wordpress fields----

//methods for fetching data from a post . [input parameter is a post]
var methods = {
//get ID of the post
    ID: function (post) {
        test.ID = post.ID;
    },
//get title of the post 
    title: function (post) {
        test.title = post.title;
    },
//get ID of the author ID   
    authorID: function (post) {
        test.author.ID = post.author.ID;
    },
//get name of the author name   
    authorName: function (post) {
        test.author.name = post.author.name;
    },
//get username of the author username   
    authorUsername: function (post) {
        test.author.username = post.author.username;
    },
//get title of the content  
    content: function (post) {
        test.content = post.content;
    },
//get links 
    links: function (post) {
        test.links = post.links;
    },
//get the featured_image guid (featured_image links) of the post
    featuredImageGuid: function (post) {

        //alert(typeof post.featured_image.guid);
        if (post.featured_image != null) {
            test.featured_image.guid = post.featured_image.guid;
        }
        else {
            test.featured_image.guid = "#";
        }
        //alert(typeof milad);
    }
}

function wpMain(post)
{
    test = jQuery.extend(true, {}, FetchedPost);
    methods.ID(post);
    methods.title(post);
    methods.authorID(post);
    methods.authorName(post);
    methods.authorUsername(post);
    methods.content(post);
    methods.links(post);
    methods.featuredImageGuid(post);
}

//fetch by ajax
$.ajax({
    url: URL,
    mimeType: "application/json",
    success: function (data, status) {
        var localData = JSON.stringify(data);
        window.localStorage.setItem('WPpost', localData);
    },
    error: function () {
        //handle the error
    }
});
var localData = JSON.parse(window.localStorage.getItem('WPpost'));
var Length = localData.length;
var posts = new Array();
$.each(localData, function (index, value) {
    wpMain(value);
    posts.push(test);
});

$.each(posts, function (index, value) {
    console.log(value.ID);
});

请进一步解释你的问题。什么是24?是每个邮资收益的ID,请进一步解释您的问题。我不明白您是如何到达24点的,以及您是如何知道它应该是34 32 20 24的。@milad-ahmadi04您应该为此制作JSFIDLE或演示工作代码。请查看此URL:我测试了您的方式。我也有同样的问题。产出:24