难以解析JSON

时间:2013-07-13 19:26:47

标签: ajax jquery

我正在尝试通过将Ajax调用回桌面版来更新桌面版网站的移动版本上的页面内容。该站点的移动版本位于专用子域中,来自该站点的桌面版本的页面内容正在打包为JSON。 Ajax调用成功,因为我能够在控制台中注销JSON对象。但是,我无法隔离JSON对象的特定部分(在对象中标记为“content”)。我写的函数不会在页面上放置任何东西(虽然它会在页面上放置其他HTML,排除函数本身有问题),并尝试记录JSON对象的“内容”部分特别是在控制台中返回“undefined”。我在整个过程中使用的代码是:

   function processJSON(url, id){
        $.ajax({
            url: url,
            data: {get_param : 'content'},
            success: function(response){
                $(id).html(response.content);                   
            },//ends success
            dataType: 'json',
        });//ends ajax
    };//ends processJSON

该函数的参数如下:“url”是JSON提要的url(通常设置为等于变量),“id”是我尝试放置的div的id JSON对象的“内容”部分的HTML。

JSON响应如下:

  {"status":"ok","page":{"id":9,"type":"page","slug":"scenes","url":"http:\/\/www.davidcharlesbrown.com\/scenes\/","status":"publish","title":"Scenes","title_plain":"Scenes","content":"<h3>I&#8217;ve always been wrapped up in the magic of the stage.<\/h3>\n<p>When I was a junior in high school, I was cast in a chorus role of my first play. I froze as I was about to make my first entrance in front of an audience of several hundred people. The person behind me pushed me on stage, which was the kindest thing he could have done.<\/p>\n<p>Since then, I&#8217;ve been involved in over 100 productions in semi-professional, educational, community, and faith-based settings. I write, direct, design, and occasionally even act. I&#8217;ve also taught acting methods.<\/p>\n<p>I&#8217;m currently a member of the Autism Theatre Network, through the <a href=\"http:\/\/www.appliedtheatrecenter.org\/autismnetwork.html\" target=\"_blank\">Applied Theatre Center<\/a>. I&#8217;m involved with an agency that uses theatre to teach social pragamatics to children, adolescents, and young adults who are on the Autism spectrum.<\/p>\n<p>It&#8217;s a stage of a different sort.<\/p>\n","excerpt":"<p>I&#8217;ve always been wrapped up in the magic of the stage. When I was a junior in high school, I was cast in a chorus role of my first play. I froze as I was about to make my first entrance in front of an audience of several hundred people. The person behind me pushed [...]<\/p>\n","date":"2013-04-10 14:08:27","modified":"2013-04-16 17:34:18","categories":[],"tags":[],"author":{"id":1,"slug":"truthscribe722gmail-com","name":"David Brown","first_name":"David","last_name":"Brown","nickname":"REDACTED","url":"","description":""},"comments":[],"attachments":[],"comment_count":0,"comment_status":"open","custom_fields":{}}}

谢谢!

1 个答案:

答案 0 :(得分:3)

content不在对象的根目录下,它位于页面属性中。

$(id).html(response.page.content);  

{
    "status": "ok",
    "page": {
        "id": 9,
        "type": "page",
        "slug": "scenes",
        "url": "http://www.davidcharlesbrown.com/scenes/",
        "status": "publish",
        "title": "Scenes",
        "title_plain": "Scenes",
        "content": "<h3>I&#8217;ve always been wrapped up in the magic of the stage.</h3>\n<p>When I was a junior in high school, I was cast in a chorus role of my first play. I froze as I was about to make my first entrance in front of an audience of several hundred people. The person behind me pushed me on stage, which was the kindest thing he could have done.</p>\n<p>Since then, I&#8217;ve been involved in over 100 productions in semi-professional, educational, community, and faith-based settings. I write, direct, design, and occasionally even act. I&#8217;ve also taught acting methods.</p>\n<p>I&#8217;m currently a member of the Autism Theatre Network, through the <a href=\"http://www.appliedtheatrecenter.org/autismnetwork.html\" target=\"_blank\">Applied Theatre Center</a>. I&#8217;m involved with an agency that uses theatre to teach social pragamatics to children, adolescents, and young adults who are on the Autism spectrum.</p>\n<p>It&#8217;s a stage of a different sort.</p>\n",
        "excerpt": "<p>I&#8217;ve always been wrapped up in the magic of the stage. When I was a junior in high school, I was cast in a chorus role of my first play. I froze as I was about to make my first entrance in front of an audience of several hundred people. The person behind me pushed [...]</p>\n",
        "date": "2013-04-10 14:08:27",
        "modified": "2013-04-16 17:34:18",
        "categories": [],
        "tags": [],
        "author": {
            "id": 1,
            "slug": "truthscribe722gmail-com",
            "name": "David Brown",
            "first_name": "David",
            "last_name": "Brown",
            "nickname": "REDACTED",
            "url": "",
            "description": ""
        },
        "comments": [],
        "attachments": [],
        "comment_count": 0,
        "comment_status": "open",
        "custom_fields": {}
    }
}