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
      }]
1
  • 1
    Please do not change the question, if you have another question then post a new question. Commented Aug 3, 2014 at 19:15

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

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  
        );  

7 Comments

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
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(...
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
can you update your question post and put more of your code to try to help you.
ok I deleted my "answer" post and updated the original question/code
|
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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.