0

I have a a JSON file with all my data in it and I want to read it to an array in my script.. below you can see an attempt, but it doesn't seem that "props" is actually getting data.. since I am new to javascript I am guessing there is something simple missing, hoping someone can tell me what I am doing wrong when loading the array.

if I call alert(record) inside the loop it outputs "0", "1", "2" incrementing during each loop, if I try to alert(record.Address1) it returns "undefined", how does this work?

var props = [];
$.getJSON('http://someurl/auction.json', function(json) {
    for (var record in json)
    {
        props.push(
            record.Address1,
            record.City,
            record.State,
            record.Zip,
            record.Bed,
            record.Bath,
            record.Price,
            record.LotSize,
            record.SQFT,
            record.YearBuilt,
            "10%",
            record.latitude,
            record.longitude,
            record.Description  
        );  
    }      
}); 

Here are the first couple records in the JSON file

    [
      {
        "TrusteeNumber":"WA-14-611878-TC",
        "Date":"8/8/2014 8:00",
        "Address1":"7616 East Nora Avenue",
        "Address2":"",
        "County":"Spokane",
        "State":"WA",
        "Zip":99212,
        "Description":"Description for 7616 East Nora Avenue, Spokane Valley, Spokane, WA 99212\n\n\n\n\n  \n Important Documents \n\n\nTrustee Sale Terms and Conditions  \n\nSample Trustee Deed Upon Sale  \n\nSample Certificate of Sale/Receipt  \n\nIRS Form 8300",
        "City":"Spokane Valley",
        "AssetType":"Residential",
        "PropertyType":"SFR",
        "Beds":3,
        "Baths":1,
        "YearBuilt":1950,
        "SQFT":"1,160",
        "LotSize":0.25,
        "APN":"45073 0265",
        "EventItemNumber":"E2010-320",
        "PropertyID":1706957,
        "Image":"http://cdn.mlhdocs.com/rcp_files/auctions/E-2010/photos/thumbnails/155290117-1_bigThumb.jpg",
        "latitude":47.672913,
        "longitude":-117.301561
      },
      {
        "TrusteeNumber":"WA-14-611501-TC",
        "Date":"8/8/2014 8:00",
        "Address1":"235 W Columbia Ave",
        "Address2":"",
        "County":"Spokane",
        "State":"WA",
        "Zip":99205,
        "Description":"Description for 235 W Columbia Ave, Spokane, Spokane, WA 99205\n\n\n\n\n  \n Important Documents \n\n\nTrustee Sale Terms and Conditions  \n\nSample Trustee Deed Upon Sale  \n\nSample Certificate of Sale/Receipt  \n\nIRS Form 8300",
        "City":"Spokane",
        "AssetType":"Residential",
        "PropertyType":"SFR",
        "Beds":3,
        "Baths":3,
        "YearBuilt":1947,
        "SQFT":"3,151",
        "LotSize":0.15,
        "APN":"36311 3601",
        "EventItemNumber":"E2010-307",
        "PropertyID":1707641,
        "Image":"http://cdn.mlhdocs.com/rcp_files/auctions/E-2010/photos/thumbnails/208185496-1_bigThumb.jpg",
        "latitude":47.7107249,
        "longitude":-117.415536
      }]
Musa
  • 96,336
  • 17
  • 118
  • 137
Garin
  • 25
  • 5
  • 1
    Please do not change the question, if you have another question then post a new question. – Musa Aug 03 '14 at 19:15

4 Answers4

1

The loop you are using to iterate the json variable doesn't behave like foreach in other programming language. Instead it behave like ordinary for loop which will iterate through via index. So to use the json collection you need to modify your code like below:

for (var record in json)
{
    props.push(
        json[record].Address1,
        json[record].City,
        json[record].State,
        ....
    );  
}  

Alternately you could try below as well and this will behave same like foreach in other programming language:

$(json).each(function(index, record) {
    props.push(
        record.Address1,
        record.City,
        record.State,
        ....
    );
});

Hope this will help.

Kundan Singh Chouhan
  • 13,952
  • 4
  • 27
  • 32
0

With for (var record in json), record isn't actually the record as is evident by the alerts of it, what it does is enumerate the property names in the object and since json is an array record is the indices of that array.
If you want the records of the array you'll have to do json[record].Address1.

I'd also suggest not using a for in loop for an array, the

for (i = 0; i < json.length; i++) 

make it much more clear that you're looping an array

Musa
  • 96,336
  • 17
  • 118
  • 137
0

Your for loop isn't used correctly, in for( var record in json ), record is the index, which is in this case 0, 1 . to make it work try this:

for (var key in json)
    {
        var record = json[key];
        props.push(
            record.Address1,
            record.City,
            record.State,
            record.Zip,
            record.Bed,
            record.Bath,
            record.Price,
            record.LotSize,
            record.SQFT,
            record.YearBuilt,
            "10%",
            record.latitude,
            record.longitude,
            record.Description  
        );  
Khalid
  • 4,730
  • 5
  • 27
  • 50
  • ok so I used this method and it populates the props array, but then immediately after leaving the routine props gets reset to null. I fill the array in the initialize() method and then try to use the props variable within the $(document).ready event, but it is blank – Garin Aug 03 '14 at 17:45
  • may be it's a scope issue, try to declare props outside everything, and inside your ajax function, instead of using `props.push(...` use `window.props.push(...` – Khalid Aug 03 '14 at 17:49
  • I posted an answer below to include the code, I was basically doing that just not using window.props.push, but it is still resetting to null – Garin Aug 03 '14 at 18:10
  • can you update your question post and put more of your code to try to help you. – Khalid Aug 03 '14 at 18:17
  • ok I deleted my "answer" post and updated the original question/code – Garin Aug 03 '14 at 18:37
  • @Garin try using `window.props` or `window['props']` in all your code and tell me if it becomes null. and remove `var props = []` from the top – Khalid Aug 03 '14 at 18:54
  • @Garin did you read the question that this question was closed as a duplicate of this one? – Musa Aug 03 '14 at 19:11
0

I have alerted the props variable in a few different places within the code and commented where it is null and where it has content. I have also added a comment on the line where I solved the problem. Basically call a function while props still has a value and assign that data to a global var from there. You cannot assign directly to the global without a function.

   <script type="text/javascript">

       var props = [], returnData;
    function initialize() {
$(document).ready(function() {
    $.ajax({
        url: "http://5i2.us/jacksonmashup/allResults.json",
        async: false,
        dataType: 'json',
        success: function (data) {
            returnData = data;
        }
    });

    var len = returnData.length;
    for (var i = 0; i < len; i++){
        var record = returnData[i];
        props.push([
            '<img src="' + record.Type + '"/>',
            record.Address1,
            record.City,
            record.State,
            record.Zip,
            record.Beds,
            record.Baths,
            record.Price,
            record.SQFT,            
            record.LotSize,
            '10%',
            record.latitude,
            record.longitude,
            record.Description,
            record.Image,
            record.Type]
        )               
    };
       });
   }
 </script>
Garin
  • 25
  • 5