DCJones DCJones - 27 days ago 11
jQuery Question

json curly brackets displaying in Div that display the json result

I have a Jquery script that does a lookup on a data table and returns a result.

However, when I display the result inside a DIV the curly brackets surrounding the json data are also displayed. Can anyone see where I have gone wrong.

My script:

$(document).ready(function()
{
$('#board').change(function(){
$.get('check_override_image.php', { RecordID: form2.board.value },
function(result) {
$('#size').html(result).show();
});
});
});


The Json result is:

{"imagesizes":[{"DisplayWidth":"1080","DisplayHeight":"1920"}]}


I am displaying the result using

<div id="size"></div>


What I want to display is DisplayWidth:1080,DisplayHeight:1920 removing all the brackets and quote marks.

Is this possible.

Many thanks for your time.

Answer

You need to JSON.parse() then access keys, values normally.

See below example:

result = JSON.parse(result);
$('#size').html("DisplayWidth: " + result["imagesizes"][0]["DisplayWidth"] 
            + ", DisplayWidth: " + result["imagesizes"][0]["DisplayWidth"]).show();
Comments