Search code examples
jqueryajaxjsonwcfodata

reading json [object object] result from ajax odata call


I am making a call to my oData WCF service hosted on my local PC and the result I get when I pass result to alert box is [object object]. The console.log(data) is showing structure as follows

[object Object]
   {
      [functions]: ,
      __proto__: {
         [functions]: ,
         __proto__: null
      },
      odata.metadata: "http://localhost:65401/sdrservice.svc/$metadata#usermasters",
      value: [
         0: {
            [functions]: ,
            __proto__: {
               [functions]: ,
               __proto__: null
            },
            company: "CBI",
            Id: 129576,
            lockcode: "azxh",
            name: "Azhar Husain"
         },
         length: 1
      ]
   }

I want to read the value of company and name from the object of the user in jquery. Please guide me how to access those fields with the exact syntax. I have already tried using data.d or data[0].company nothing works for me.


Solution

  • Your JSON structure has a property called value, which is an array. To access the company and name of the first element in this property:

    var company = data.value[0].company;
    var name = data.value[0].name;