2

I create website to buy and sell cars. I used Edmunds API to get car information.

I tried to parse information into drop list from their API site as JSON format but I did not get result . I do not how to get information from nested arrays how to get name, nickName , year (in the image)?

enter code here
<!DOCTYPE html>
<html>

<body>

<h1>Customers</h1>
<select id="dd"></select>

<script>
var xmlhttp = new XMLHttpRequest();
var url = "https://api.edmunds.com/api/vehicle/v2/makes?state=used&year=2014&view=basic&fmt=json&api_key=xxxxxxxxxxxxxxxxxxxxxxxxxx";

xmlhttp.onreadystatechange=function() {
    if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
        myFunction(xmlhttp.responseText);
    }

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

function myFunction(response) {
    var items = JSON.parse(response);
    var i;
    var out = "<table>";



    function addOptions(){
        JSONObject object = new JSONObject();
        var jsonArray = object.getJSONArray("url");
        for (var i = 0; i < jsonArray.length; i++) {
        option = document.createElement('option');
              option.text = jsonArray.makes[0].name;
              select.add(option);
}
</script>

</body>
</html>

enter image description here

Mohammed
  • 51
  • 1
  • 8
  • Are you missing some code? that isn't valid JS... You also never call `addOptions()`, you never loop over `items`... it seems like some code is missing. – CodingWithSpike Apr 02 '16 at 17:09
  • Related: [Access / process (nested) objects, arrays or JSON](https://stackoverflow.com/questions/11922383/access-process-nested-objects-arrays-or-json) – Jonathan Lonowski Apr 03 '16 at 03:46

2 Answers2

0

You didn't close addOptions() and his for loop inside's brackets.

boxHiccup
  • 128
  • 8
0
var arrTest = items.makes;

            for (var i = 0; i < arrTest.length; i++) {


                var oneObj = arrTest[i];

                var strName = oneObj.name;
                var niceName = oneObj.niceName;

                var arrModel = oneObj.models;

                for (var j = 0; j < arrModel.length; j++) {

                    var strName1 = arrModel[j].name;
                    var niceName1 = arrModel[j].niceName;
                      debugger; 
                    var arrYear = arrModel[j].years[0];
                }
            }

You can loop arrModel[j].nickName[0].year if there are multiple elements

RIYAJ KHAN
  • 15,032
  • 5
  • 31
  • 53