0

I have the following code to get the results of a query. The query results are in the form of JSON.

$.ajax({
    url: ..... //url//...,
    contentType: "application/json; charset=utf-8",
    data: JSON.stringify({
        "type": "daysofyear",
        "entity": {
            "year": "2015"
        }
    }),
    type: "POST",
    dataType: "json",
    success: function(result) {
        if ((result) && (result.isSuccess == true)) {
            alert("SUCCESS");
            alert(json.entity.day);
        }
    },
});

The returned JSON is:

{
    "isSuccess": true,
    "results": [{
        "jsonClass": "RuleSuccess",
        "message": "Found mapping for year: 2015",
        "rule": {
            "name": "Year rule",
            "metadata": {}
        },
        "entity": {
            "year": "2015",
            "month": "December",
            "day": "Saturday"
        }
    }]
}

Im basically trying to extract the value of month, year, day separately and display them alone using

alert(json.entity.day);

Please advice.

photo_tom
  • 7,292
  • 14
  • 68
  • 116
pal
  • 105
  • 1
  • 3
  • 17

2 Answers2

3

Update

var a =JSON.parse('{"isSuccess":true,"results":[{"jsonClass":"RuleSuccess","message":"Found mapping for year: 2015","rule": {"name":"Year rule","metadata":{}}, "entity":{"year":"2015", "month":"December",  "day":"Saturday"}}]}') ;


alert(a.results[0].entity.day);

Original

Use

alert(result[0].rule.entity.day);
Tushar Gupta
  • 15,504
  • 1
  • 29
  • 47
0

try this:

obj = JSON.parse(json);
console.log(obj[0].rule.entity.day);
ErasmoOliveira
  • 1,416
  • 2
  • 20
  • 40
  • 2
    The response is auto-parsed by jQuery since the `dataType` was set to JSON - Also, `obj` is an array, so `obj[0]` – tymeJV Mar 04 '15 at 19:11