CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   DOM and JSON scripting (http://www.codingforums.com/forumdisplay.php?f=15)
-   -   How to Transverse JSON with jQuery (Bountify)) (http://www.codingforums.com/showthread.php?t=277790)

earthlyKitt 10-19-2012 08:21 AM

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

devnull69 10-19-2012 10:47 AM

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" : "", ...}}}



All times are GMT +1. The time now is 06:52 PM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.