Sush
Sush

Reputation: 1457

get keys and values from a json data

  result=  {
        "_id": "5319b5e10748a6078fe4f360",
        "acces": "172.1.6.2.18",        
        "results": [{
            "\"test\":\"Connect_Disconnect\",\"os\":\"Windows NT\",\"report\":\"Verify Wireless Interface present and state is Disconnected:OK<br>Verify Profile not present on the Client:OK<br>Verify the State is Disconnected:OK<br>Delete Profile to the Client:OK<br>Verify Profile Not Present on the Client:OK<br>\"": ""
        }],
        "tests": "Test01"
    }

while i trying to alert following

alert(JSON.stringify(result.results[0]));

am getting following data

{
    "\"test\":\"Connect_Disconnect\",\"os\":\"Windows NT\",\"report\":\"Verify Wireless Interface present and state is Disconnected:OK<br>Verify Profile not present on the Client:OK<br>Add Profile to the Client:OK<br>Verify Profile Added Present on the Client:OK<br>Connecting to Access Point:OK<br>Verify the State is Connected:OK<br>Disconnecting from Access Point:OK<br>Verify the State is Disconnected:OK<br>Delete Profile to the Client:OK<br>Verify Profile Not Present on the Client:OK<br>\"": ""
}

from this how can i get the values of test,os and report

i tried

result.results[0].test 

but getting value as undefined.

Here is the updated part

     $.get('/getStatus', getdata, function (data) {
           data.forEach(function (testreport) {
var report = JSON.stringify(testreport);
alert(report);

        }); 


      });
here alert prints



 {
        "_id": "5319b5e10748a6078fe4f360",
        "acces": "172.1.6.2.18",
        "adapter": "Win 10",
        "flavour": "VM-IE8-001-preq1",
        "id": "67",
        "os": "VM-WIN7-64",
        "results": [{
            "\"test\":\"Connect_Disconnect\",\"os\":\"Windows NT\",\"report\":\"Verify Wireless Interface present and state is Disconnected:OK<br>Verify Profile not present on the Client:OK<br>Add Profile to the Client:OK<br>Verify Profile Added Present on the Client:OK<br>Connecting to Access Point:OK<br>Verify the State is Connected:OK<br>Disconnecting from Access Point:OK<br>Verify the State is Disconnected:OK<br>Delete Profile to the Client:OK<br>Verify Profile Not Present on the Client:OK<br>\"": ""
        }],
        "tests": "Test01"
    }

Upvotes: 0

Views: 72

Answers (3)

Chintan Soni
Chintan Soni

Reputation: 25287

Can you just try using getJSON() method of jquery, and implement it this way:

$.getJSON( "enterYourUrlHere", function( result ) {
    alert(result.results[0].test)
});

And, Yes ofcourse, @Mate is correct.

Upvotes: 0

Mate
Mate

Reputation: 5284

Your result.results it's a string, not a json.

check DEMO

var results = {
    "\"test\":\"Connect_Disconnect\",\"os\":\"Windows NT\",\"report\":\"Verify Wireless Interface present and state is Disconnected:OK<br>Verify Profile not present on the Client:OK<br>Add Profile to the Client:OK<br>Verify Profile Added Present on the Client:OK<br>Connecting to Access Point:OK<br>Verify the State is Connected:OK<br>Disconnecting from Access Point:OK<br>Verify the State is Disconnected:OK<br>Delete Profile to the Client:OK<br>Verify Profile Not Present on the Client:OK<br>\"": ""
};


var resultsValid = {
    "test": "\"Connect_Disconnect\"",
    "os": "\"Windows NT\""
};


alert(results.test)

alert(resultsValid.test)
alert(resultsValid.os)

Check how are you generating this response

Also, you can verify json data on http://jsonlint.com/

Upvotes: 1

momo
momo

Reputation: 3935

it's an object, not an array. there's no 0 index, just a hash or properties. use this:

result.test

Upvotes: 0

Related Questions