JQuery getJSON - ajax parseerror

后端 未结 17 1910
南笙
南笙 2020-12-01 13:04

I\'ve tried to parse the following json response with both the JQuery getJSON and ajax:

[{\"iId\":\"1\",\"heading\":\"Management Services\",\"body\":\"

17条回答
  •  南笙
    南笙 (楼主)
    2020-12-01 13:42

    The JSON string you have is an array with 1 object inside of it, so to access the object you have to access the array first. With a json.php that looks like this:

    [
        {
            "iId": "1",
            "heading": "Management Services",
            "body": "

    Program Overview

    January 29, 2009

    " } ]

    I just tried this

    $.getJSON("json.php", function(json) {
        alert(json[0].body); // 

    Program Overview

    January 29, 2009

    alert(json[0].heading); // "Management Services" alert(json[0].iId); // "1" });

    I also tried this:

    $.get("json.php", function(data){
        json = eval(data);
        alert(json[0].body); // 

    Program Overview

    January 29, 2009

    alert(json[0].heading); // "Management Services" alert(json[0].iId); // "1" });

    And they both worked fine for me.

提交回复
热议问题