3

I have a data1.json file

[ { "BID" : "4569749", }, { "BID" : "466759", }, { "BID" : "4561149", }, ]

I want to call this .json file inside another abdv.js file and assign the BIDs to a variable array

var R1;

i.e the value of R1 = ['4569749', '466759', '4561149'] How can i do this??

.html file(please note: I am specifying only those parts relevant to this quetsion and erased other parts)

Code follows:

<script src="http://code.jquery.com/jquery-1.10.2.min.js"></script>
<script src="abdv.js"></script>
<script>

    loadABCD();

   </script>

abvd.js

Code follows:

function loadABCD()
    {


  var R1 = [];
$.get('data1.json', function (data) {
    var parsedArr = JSON.parse(data),
    i = 0, l = parsedArr.length;
    for (i; i < l; i++) {
        R1.push(parsedArr[i].BID);
    }
});




for(k=0;k<R1.length;k++)
 {
    document.write(R1);
}   
        // i will use R1 for later processing another function

    //  for(k=0; k<R1.length; k++)
    //  {
    //  obj = loadLevel4Obj(R1[k]);
    //  scene.add(obj);
    //    } 




    }

I also need help to read .json file into a javascript JSON object. I doubt whether the format of my json file is correct. Do i need to specify any other jquery related libs in the html file?? How do i check whether the BIDs are correctly stored in the variable R1.

3 Answers 3

3

Assuming you've read your json file into a javascript JSON object (do you need help with that as well?)

var R1 = new Array();    

for(var i= 0 ; i< myJSON.length; i++){
   R1.push(myJSON[i].BID);
}

[EDIT]

Your document.write is happening before you've done any reading.

Remove it.

put a console.log instead in your anonymous callback from your $.get();

Make it look like this:

$.get('data1.json', function (data) {
    var parsedArr = JSON.parse(data),

    for (i = 0; i < parsedArr.length; i++) {
        R1.push(parsedArr[i].BID);
    }


    for(i=0; i< R1.length; i++)
      console.log("item " + i + " = " + R1[i]);
});

Also if your json file really looks like this:

[ { "BID" : "4569749", }, { "BID" : "466759", }, { "BID" : "4561149", }, ]

Then it needs to be fixed to remove the extraneous commas inside your objects. It should look like this:

[ { "BID" : "4569749" }, { "BID" : "466759" }, { "BID" : "4561149" } ]

I haven't tested this, but it looks like it should work to me.

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

3 Comments

for..in loop throw array? You will get in this loop all shared properties from Array.prototype.
I could not get it working: I will be more specific to my problem
You're doing a bunch of small things wrong... I'll edit my answer.
0
var R1 = [];
$.get('file.json', function (data) {
    var parsedArr = JSON.parse(data),
    i = 0, l = parsedArr.length;
    for (i; i < l; i++) {
        R1.push(parsedArr[i].BID);
    }
});

1 Comment

The OP makes no mention of jQuery... please edit your answer to remove said reference.
-1

Getting the json file with ajax, then process it and assign to R1:

function ajaxGetJson(url, callback){
    var xmlhttp = new XMLHttpRequest();
    xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            callback(eval(xmlhttp.responseText));
        }
    }

    xmlhttp.open("GET", url, true);
    xmlhttp.send();
}

var R1;
ajaxGetJson('file.json', function(obj){
   R1 = obj.map(function(item){ return item.BID });
});

Some comments:

  • I'm using eval instead of JSON.parse because your .json may not be in strict json format (as your example)

  • As the question has tag html5, your browser for sure supports the .map method

Cheers, from La Paz, Bolivia

1 Comment

@Pinal Why? His Json file isn't in json strict format, so using JSON.parse would give you parse error. Eval can be used if it's used wisely as in this case.

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.