Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Oct 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    How to Transverse JSON with jQuery (Bountify))

    Please help
    I am familiar with iterating through XML but need guidance transitioning to JSON.
    I'm sure this is simple but I've looked all over the "Internets" only to find overly complicated examples. Please help me :
    1.externally load a file named "myStuff.json" via jQuery Ajax get
    2.Transverse through the document with jQuery grabbing all elements
    3.display in an unordered list. I need it's JSON equivalent

    See Full code at...
    https://bountify.co/R


    MY XML: pullData2.xml
    <objects>
    <person>
    <name>John</name>
    <street>Oak Way</street>
    <kids>
    <kid1>
    <kidName>Amber</kidName>
    </kid1>
    <kid2>
    <kidName>Jessy</kidName>
    </kid2>
    </kids>
    </person>
    </objects>

    MY jQuery:

    $.get('pullData2.xml', function(d){
    $('#loadXML').append('<dl id="shufflePeople"/>');
    $(d).find('person').each(function(){
    var personName = $person.find('name').text(); ... ETC

  • #2
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,355
    Thanks
    11
    Thanked 558 Times in 551 Posts
    Usually you can transform <xml> elements that can appear only once
    Code:
    from
    <ELEMENTNAME>
    value
    </ELEMENTNAME>
    
    into
    {"ELEMENTNAME" : "value"}
    Elements that can occur more than once will have to be an array inside of JSON
    Code:
    <kids>
      <kid>
        <kidName>Jessy</kidname>
      </kid>
      <kid>
        <kidName>Amber</kidname>
      </kid>
    </kids>
    
    will become
    {"kids": [{"kidName": "Jessy"}, {"kidName" : "Amber"}]}
    Elements with no value (only subelements) will be nested like this
    Code:
    <objects>
    <person>
    ...
    </person>
    </objects>
    
    {"objects":{"person" : {"name" : "", ...}}}


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •