简体   繁体   English

从json数组中获取数据

[英]fetch data from json array

please see this URL: " http://www.zaferteam.com/wp-json/posts/ " .请查看此 URL:“ http://www.zaferteam.com/wp-json/posts/ ”。 it's json array of my website posts.它是我网站帖子的 json 数组。 i want to store each member of this array in posts array.我想将此数组的每个成员存储在 posts 数组中。 at last in posts array will be all posts of my website in json format.最后,posts 数组中将是我网站的所有 json 格式的帖子。 at the end of the code i try to get the ID of each post.在代码的末尾,我尝试获取每个帖子的 ID。 my posts ID are in-order: 34 32 20 24 but it shows: 24 24 24 24 please help me, thanks.我的帖子 ID 是有序的:34 32 20 24 但它显示:24 24 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);
            }); 
    });

Please try following approach:请尝试以下方法:

create new function function readLocalStorageData() as follows:创建新函数function 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);
    }); 
}

now call above function your ajax success handler as follows:现在调用上面的函数你的ajax success处理程序如下:

//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
        }
    });

This works fine with a local json file:这适用于本地 json 文件:

Added the test var...添加了测试变量...

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);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM