-1

I am trying to parse this JSON that I have assigned to a javascript variable but I am having a hard time looping through it due to it having multiple levels

<script type="text/javascript">

        var varJson = {
            "d": {
                "results": [
                    {
                        "__metadata": {
                            "id": "http://www.bradinsight.com/ALFAPI/AlfWebApi/Offices(100013449)",
                            "uri": "http://www.bradinsight.com/ALFAPI/AlfWebApi/Offices(100013449)",
                            "type": "AlfWebApiInfrastructure.Poco.Office"
                        },
                        "Agency": {
                            "__deferred": {
                                "uri": "http://www.bradinsight.com/ALFAPI/AlfWebApi/Offices(100013449)/Agency"
                            }
                        },
                        "Advertiser": {
                            "__deferred": {
                                "uri": "http://www.bradinsight.com/ALFAPI/AlfWebApi/Offices(100013449)/Advertiser"
                            }
                        },
                        "Contacts": {
                            "results": [
                                {
                                    "__metadata": {
                                        "id": "http://www.bradinsight.com/ALFAPI/AlfWebApi/Contacts(id=59980,jobId=1000105450)",
                                        "uri": "http://www.bradinsight.com/ALFAPI/AlfWebApi/Contacts(id=59980,jobId=1000105450)",
                                        "type": "AlfWebApiInfrastructure.Poco.Contact"
                                    },
                                    "id": 59980,
                                    "jobId": 1000105450,
                                    "mask": 67108863,
                                    "name": "Name",
                                    "jobtitle": "Job Title",
                                    "email": "email",
                                    "companyType": "companyType",
                                    "companyId": 1787,
                                    "officeId": 100013449,
                                    "address1": "address1",
                                    "address2": "",
                                    "address3": "",
                                    "address4": "",
                                    "addressTown": "addressTown",
                                    "addressPostCode": "addressPostCode",
                                    "tel": "tel",
                                    "fax": "fax",
                                    "responsibilities": "responsibilities"
                                },
                                {
                                    "__metadata": {
                                        "id": "http://www.bradinsight.com/ALFAPI/AlfWebApi/Contacts(id=64085,jobId=1000105448)",
                                        "uri": "http://www.bradinsight.com/ALFAPI/AlfWebApi/Contacts(id=64085,jobId=1000105448)",
                                        "type": "AlfWebApiInfrastructure.Poco.Contact"
                                    },
                                    "id": 64085,
                                    "jobId": 1000105448,
                                    "mask": 67108863,
                                    "name": "name",
                                    "jobtitle": "jobtitle",
                                    "email": "email",
                                    "companyType": "companyType",
                                    "companyId": 1787,
                                    "officeId": 100013449,
                                    "address1": "address1",
                                    "address2": "",
                                    "address3": "",
                                    "address4": "",
                                    "addressTown": "addressTown",
                                    "addressPostCode": "addressPostCode",
                                    "tel": "tel",
                                    "fax": "fax",
                                    "responsibilities": "responsibilities"
                                }
                            ]
                        },
                        "id": 100013449,
                        "companyId": 1787,
                        "addressLine1": "addressLine1",
                        "addressLine2": "",
                        "addressLine3": "",
                        "addressLine4": "",
                        "addressLine5": "addressLine5",
                        "postCode": "postCode",
                        "regionId": "regionId",
                        "countyId": "countyId",
                        "tvAreaId": "L",
                        "telephone": "telephone",
                        "fax": "fax8",
                        "countryId": "countryId",
                        "email": "email",
                        "isdn": null,
                        "mask": 66060287
                    }
                ]
            }
        };
        
               

        $(document).ready(function () {


            //var parsed = JSON.parse(varJson);

            alert(varJson);

        

        });

    </script>

I tried using this:

$.each(varJson, function (index, item) {
              alert(item);

                });

But it just says object in my alert.

Cesar M
  • 101
  • 3
  • 12
nick gowdy
  • 6,191
  • 25
  • 88
  • 157
  • 3
    That is not JSON, it's a JavaScript "object literal". It's already "parsed" - I think you meant "traverse". – Alnitak Apr 11 '13 at 12:28
  • @Alnitak Sorry I mean traverse because I want to see the contents of my object literal in either an alert or console. – nick gowdy Apr 11 '13 at 12:29

2 Answers2

2

try this,

function traverse(jsonObj) {
  if( typeof jsonObj == "object" ) {
    $.each(jsonObj, function(k,v) {
        traverse(v);
    });
  }
  else {
    alert(jsonObj);
  }
}

$(document).ready(function () {
   traverse(varJson);
});

http://jsfiddle.net/satpalsingh/hXEr8/

with reference of Traverse all the Nodes of a JSON Object Tree with JavaScript

Community
  • 1
  • 1
Satpal
  • 132,252
  • 13
  • 159
  • 168
1

You don't need to parse it in your example.

Use console.log instead of alert and you will the the object in your web inspector.


If you do want to alert do something like this

alert(varJson.d.results[0].__metadata.id)

Blowsie
  • 40,239
  • 15
  • 88
  • 108
  • When I use $.each to loop through my JSON I get LOG: [object Object]. This is when I use $.each(varJson, function (index, item) { console.log(item); }); – nick gowdy Apr 11 '13 at 12:28
  • What browser are you using? No need to loop in chrome or firebug – Blowsie Apr 11 '13 at 12:29