如何调用JSON对象?

时间:2015-12-01 16:42:56

标签: javascript php json

我在调用特定对象时遇到问题。例如,我想调用mydata.title

我的php里面有ff(这只是代码的一部分)

while($row = $DB->result->fetch_assoc()){

        $Fthis[] = array( 

            "title" => $row["title"],
            "subtitle" => $row["subtitle"],
            "dates" => $row["dates"],
            "Author" => $row["Author"],
            "content" => $row["content"],
            "himgsrc" => $row["himgsrc"]
        );

          array_push($marxarray, $Fthis);
    }

    echo json_encode($marxarray);

然后我在我的js中有这个AJAX。

$.ajax({
  url: 'editer.php',
  type: 'POST',
  data: { id: blogid },
  success: function (data) {
    var mydata= JSON.parse(data);
    console.log(mydata);
    alert(mydata.title);
  }
});

为什么警报返回undefined? 如果我查看日志,我可以看到数组已经正确传递,所以php没问题(我认为)。

这里看起来是怎样的。

enter image description here

1 个答案:

答案 0 :(得分:1)

我检查了你的图片,你必须使用数组索引

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:security="http://www.springframework.org/schema/security"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="
           http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd

http://www.springframework.org/schema/security

           http://www.springframework.org/schema/security/spring-security-3.0.xsd">

    <security:http auto-config='true'>
        <security:intercept-url pattern="/api/**" access="ROLE_USER"/>
        <security:intercept-url pattern="/**" access="IS_AUTHENTICATED_ANONYMOUSLY"/>
        <security:http-basic />
    </security:http>

    <security:authentication-manager>
        <security:authentication-provider>
            <security:user-service>
                <security:user name="user" password="password1" authorities="ROLE_USER" />
            </security:user-service>
        </security:authentication-provider>
    </security:authentication-manager>

</beans>