dtcd27183
2017-02-13 17:29
浏览 86
已采纳

使用jquery解析JSON响应中的项目

I am using PHP and Ajax to parse some JSON. The PHP is creating the array like this.

$myObj->pid = $_POST["parentid"];
$myObj->comp = $comp;
$myObj->colour = $statuscolour;
$myJSON = json_encode($myObj);
header('Content-Type: application/json');
echo $myJSON;

I use the following jquery code

$.ajax({
        type: "POST",
        dataType: "json",
        url: "msstatup.php",
        data: data
    }).done(function(msg) {
    var response = jQuery.parseJSON(JSON.stringify(msg));
    console.log(response);
    pid = response[0].pid;
    console.log('id = ' + pid);
    });

I can see the output from the first console.log as

Object {pid: "p1", comp: 20, colour: "red"}

However I cannot extract the individual variables, it gives the message

Uncaught TypeError: Cannot read property 'pid' 

How can I extract the variable?

图片转代码服务由CSDN问答提供 功能建议

我使用PHP和Ajax来解析一些JSON。 PHP正在创建这样的数组。

  $ myObj-> pid = $ _POST [“parentid”]; 
 $ myObj-> comp = $ comp;  
 $ myObj-> color = $ statuscolour; 
 $ myJSON = json_encode($ myObj); 
header('Content-Type:application / json'); 
echo $ myJSON; 
   
 
 

我使用以下jquery代码

  $ .ajax({
 type:“POST”,
 dataType:“json”,  
 url:“msstatup.php”,
 data:data 
})。done(function(msg){
 var response = jQuery.parseJSON(JSON.stringify(msg)); 
 console.log( 响应); 
 pid = response [0] .pid; 
 console.log('id ='+ pid); 
}); 
   
 
 

我可以看到第一个console.log的输出为

  Object {pid:“p1”,comp:20,color:“red”} 
    
 
 

但是我无法提取单个变量,它会给出消息

  Uncaught TypeError:无法读取属性'pid'
   
 
 

如何提取变量?

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • doubai9014 2017-02-13 17:36
    已采纳

    You are returning an object, not an array.

    Also it makes no sense to stringify the data object and parse that string back to object again

    Try

    var pid = msg.pid;
    console.log('id = ' + pid);
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • dpmir1988 2017-02-13 17:34

    First of all, it can't imagine why it would be neccessary to first stringify, then parse the JSON response.

    Second, you are trying to access response[0] as if it were an array, which it isn't. You can simply use

    response.pid;
    

    To access the object key.

    As you don't need to stringify then parse the response, you can just access msg directly, so it all comes down to

    $.ajax({
        type: "POST",
        dataType: "json",
        url: "msstatup.php",
        data: data
    }).done(function(msg) {
        console.log(msg.pid);
    });
    
    评论
    解决 无用
    打赏 举报
  • drrw8672 2017-02-13 17:36

    You've made this more complicated than it needs to be. msg is already an object, which you then convert to a string and back to an object with stringify and parseJSON. And then you try to use it like an array, when it is an object.

    Try this:

    $.ajax({
        type: "POST",
        dataType: "json",
        url: "msstatup.php",
        data: data
    }).done(function(msg) {
        var pid = msg.pid;
        console.log('id = ' + pid);
    });
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题