Sagar Yadav Sagar Yadav - 2 months ago 4
JSON Question

How to parse following json in jquery ajax success function

The following JSON data retrieved from php script and now i want to parse this JSON data. Please someone help me to parse this JSON data.

[{
"0": "1.0000",
"AVG(Q1)": "1.0000",
"1": "1.8000",
"AVG(Q2)": "1.8000",
"2": "2.0000",
"AVG(Q3)": "2.0000",
"3": "2.4000",
"AVG(Q4)": "2.4000",
"4": "1.6000",
"AVG(Q5)": "1.6000",
"5": "1.2000",
"AVG(Q6)": "1.2000",
"6": "2.0000",
"AVG(Q7)": "2.0000",
"7": "2.4000",
"AVG(Q8)": "2.4000",
"8": "0.8000",
"AVG(Q9)": "0.8000",
"9": "2.8000",
"AVG(Q10)": "2.8000",
"10": "1.8000",
"AVG(Q11)": "1.8000"
}, {
"0": null,
"AVG(Q1)": null,
"1": null,
"AVG(Q2)": null,
"2": null,
"AVG(Q3)": null,
"3": null,
"AVG(Q4)": null,
"4": null,
"AVG(Q5)": null,
"5": null,
"AVG(Q6)": null,
"6": null,
"AVG(Q7)": null,
"7": null,
"AVG(Q8)": null,
"8": null,
"AVG(Q9)": null,
"9": null,
"AVG(Q10)": null,
"10": null,
"AVG(Q11)": null
}]


I tried different ways to parse this JSON but all methods failed. Please help me to parse this JSON.

I tried this but no value is displayed in the table element:

var obj = jQuery.parseJSON(data);
$.each(obj, function(key, value){
$('table').append('<tr><td>'+value.avg(q1)+'</td></tr>');
});

Answer

Please check snippet. No need to parse json.

var jsobObj = [{
    "0": "1.0000",
    "AVG(Q1)": "1.0000",
    "1": "1.8000",
    "AVG(Q2)": "1.8000",
    "2": "2.0000",
    "AVG(Q3)": "2.0000",
    "3": "2.4000",
    "AVG(Q4)": "2.4000",
    "4": "1.6000",
    "AVG(Q5)": "1.6000",
    "5": "1.2000",
    "AVG(Q6)": "1.2000",
    "6": "2.0000",
    "AVG(Q7)": "2.0000",
    "7": "2.4000",
    "AVG(Q8)": "2.4000",
    "8": "0.8000",
    "AVG(Q9)": "0.8000",
    "9": "2.8000",
    "AVG(Q10)": "2.8000",
    "10": "1.8000",
    "AVG(Q11)": "1.8000"
}, {
    "0": null,
    "AVG(Q1)": null,
    "1": null,
    "AVG(Q2)": null,
    "2": null,
    "AVG(Q3)": null,
    "3": null,
    "AVG(Q4)": null,
    "4": null,
    "AVG(Q5)": null,
    "5": null,
    "AVG(Q6)": null,
    "6": null,
    "AVG(Q7)": null,
    "7": null,
    "AVG(Q8)": null,
    "8": null,
    "AVG(Q9)": null,
    "9": null,
    "AVG(Q10)": null,
    "10": null,
    "AVG(Q11)": null
}];


$.each(jsobObj, function(key, value){          
    $('table').append('<tr><td>'+(value["AVG(Q1)"]==null?"":value["AVG(Q1)"])+'</td></tr>');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
  </table>

Comments