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"
    }

Answers


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/


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

result.test


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.


Need Your Help

willselectAnnotationView missing

iphone mkannotationview ios5.1 mkmapviewdelegate

In the MKMapViewDelegate, the code in the method mapView:didSelectAnnotationView is executed AFTER the selection of the annotation view (and therefore after the callout bubble appears).

Scrollview in Cocos2d - android

android cocos2d-iphone cocos2d-android

How to Implement Scrolling functionality in cocos2d Surfaceview in android? Please give me some suggestion.