Go Back   CodingForums.com > :: Client side development > JavaScript programming > DOM and JSON scripting

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-19-2012, 08:21 AM   PM User | #1
earthlyKitt
New to the CF scene

 
Join Date: Oct 2012
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
earthlyKitt is an unknown quantity at this point
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
earthlyKitt is offline   Reply With Quote
Old 10-19-2012, 10:47 AM   PM User | #2
devnull69
Senior Coder

 
Join Date: Dec 2010
Posts: 2,245
Thanks: 10
Thanked 531 Times in 525 Posts
devnull69 will become famous soon enough
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" : "", ...}}}
devnull69 is offline   Reply With Quote
Reply

Bookmarks

Tags
ajax, javascript, jquery, json

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:40 AM.


Advertisement
Log in to turn off these ads.