【问题标题】:Acess JSON object correctly正确访问 JSON 对象
【发布时间】:2014-02-05 12:29:31
【问题描述】:

我正在尝试访问从服务器返回的 JSON 对象中的元素。 JSON的结构如下:

    ajaxResponse: 
           {
           "user":{
                "0":"Matt",
                "name":"Matt",
                 "1":"/images\/peopleImages\/test.jpg",
                 "image":"/images\/peopleImages\/test.jpg"
                },

           "comment":{
                "userCommentID":21,
                "userComment":"vc ",
                "userID":1
              },

         "error":false

        }

我已经尝试了几乎所有我能想到的:

              ajaxResponse[0].user.name
              ajaxResponse.user.name
              ajaxResponse.user['name']

但是什么都没有,我总是得到类型错误是未定义的。 任何帮助将不胜感激!

编辑

这就是我获取 JSON 的方式

          $.post( "script.php" ,
                {
                    task: 'commentInsert',
                    userId: commentUserId,
                    postId: '2',
                    userComment: comment                    
                }                                   
            ).success(
                function(ajaxResponse){     
                    console.log("response: " + ajaxResponse);
                    console.log(ajaxResponse[0].user.name);
                    /*
                    ajaxResponse[0].user.name
                    ajaxResponse[0].user.image

                    ajaxResponse[0].comment.userCommentID
                    ajaxResponse[0].comment.userComment
                    ajaxResponse[0].comment.userId
                    insertComment($.parseJSON(ajaxResponse));                   
                            */      
                }   
            ).error(
                function(){             
                    console.log("error" );              
                }   
            );

【问题讨论】:

  • 在1" 缺少" ....然后ajaxResponse: ???
  • 您是否获得了ajaxResponse.user 的有效对象?
  • 首先在此处检查您的 JSON:jsonlint.com
  • 您确定 ajaxResponse 的定义符合您的预期吗?
  • 这只是一个错字——在我回来的 json 中它很好——我刚刚在此处对其进行了重组以便于阅读。是的 ajaxResponse 是正确的 - 我无法正确访问它。

标签: php jquery arrays json element


【解决方案1】:

好的,我忘了打电话给parseJSON

      var data = $.parseJSON(ajaxResponse);

      console.log("response2: " + data.user.name);

这似乎纠正了它,并允许我正确地取回名称。

【讨论】:

    【解决方案2】:

    从您的服务器返回内容时:

    echo json_encode($ajaxResponse);
    

    在发送 POST 时:

    dataType: "json"
    

    基于此,您可能会得到它:

    ajaxResponse.user
    

    试着安慰它。

    【讨论】:

      【解决方案3】:

      对于直接使用 Json 对象,您需要告知 dataType = "json",然后您可以使用 jQuery 中的 $.ajax 来发送 post 值和正确的数据类型。

      在此处查看文档:http://api.jquery.com/jquery.post/

      $.ajax({
          type: "POST",
          url: "script.php",
          data: {
                task: 'commentInsert',
                userId: commentUserId,
                postId: '2',
                userComment: comment
          }
          success:  function(ajaxResponse){     
                      console.log("response: " + ajaxResponse);
                      console.log(ajaxResponse[0].user.name);    
                  }   ,
          dataType: "json"
      });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-10-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-11-25
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多