WordPress.org

Ready to get started?Download WordPress

Forums

XML or JSON Parsing (2 posts)

  1. davebos
    Member
    Posted 1 year ago #

    Ok, so I have been banging my head against the wall trying to figure out how to pull in select XML or JSON data into my website. I have very limited coding ability so most of what's online goes a bit over my head. I've been reading everything possible. I've really found nothing for XML, but two potential solutions for json that I can't get working.

    First is simply using the json_decode command. Problem is, the syntax is too complicated for me.

    So I am supposed to type
    json_decode ( string $json [, bool $assoc = false [, int $depth = 512 [, int $options = 0 ]]] ) somewhere. Well, where do I put the URI? I assume it is passed through some string, but how? Even better, how do I figure out how to put in the key. Is the key just one word, or does it nest? If it nests, do you put periods between?

    Let's say I figure that out. Would I put this somewhere in my child functions.php? Would I then call it into init? Let's say I do all that, what would I type into the simple html space of a wordpress post that would display (you programmers call that 'echoing'?) it in there?

    Do I need to use ajax? Do I need to enque a script or something? Not that I know what I just said...

    I feel like I almost have it, I just need someone to give me a live example of code including where it goes to make the final leap.

    I have also tried to use the JSON Data Shortcode plugin which actually seems simple enough for even me to use, but couldn't get that to display anything, though at least it kind of made sense. It simply asks me for the key. is there a prefix to the key I need or something? Let's say that I want "title" that is nested under "feed". Is the key simply "feed.title"

    Or does the plugin not work? Given it has 1 out of 5 stars, I am suspicious.

    Any help would be appreciated.

    Thanks!!

    My site
    Feed that I am trying to parse.

  2. davebos
    Member
    Posted 1 year ago #

    So I've been learning some more, but still need help. I think my trouble is with nesting. I wrote my own short code and did this:

    //Test code for json parsing
    function test_func( $atts ){
    $json = file_get_contents('https://affiliate.creditkarma.com/api/v1/offers/card/CCAmericanExpressCard54-json?pubKey=JIA7EF9VEK5X0R5J');
    $obj = json_decode($json);
    return $obj ->{'status'};
    }
    add_shortcode( 'testcode', 'test_func' );

    When I put "[testcode]" on a post, I get "SUCCESS" back which is what is stored under "status" on the json page. So far, so good. Now I try to access more interesting information that is nested in there. I have tried:

    return $obj ->{'header.name'};
    return $obj ->{'name'};
    return $obj ->{'header(name)'};
    return $obj ->{'header('name')'};
    return $obj ->{'header[name]'};

    none of which worked. I feel I am so close and just need help formatting the key. I realized I was having the same issue with the short-code plugin. when I just put "status" in as the key it worked fine.

Topic Closed

This topic has been closed to new replies.

About this Topic