JS解析JSON。未定义时的错误

时间:2018-10-13 13:34:59

标签: javascript json parsing

这是在服务器端生成的JSON代码,并通过websocket作为字符串发送给客户端:

{
    "zw_ob-k7g8ahcvFzAAAA":{
        "position":{  
             "x":319,
             "y":135
        },
        "mass":324,
        "name":21,
        "color":"#5058a9"
    },
    "uTFD8dw0OiqD4ErzAAAB":{
        "position":{  
            "x":473,
            "y":348
        },
        "mass":59,
        "name":7,
        "color":"#e79448"
    },
    "CvMRJ6rEFYoU1vajAAAC":{
        "position":{  
            "x":25,
            "y":604
      },
        "mass":147,
        "name":18,
        "color":"#cef6c3"
    },
    "_e6TLrfubHqf-7esAAAD":{
        "position":{  
            "x":146,
            "y":417
        },
        "mass":320,
        "name":13,
        "color":"#ab7aa0"
    }
}

在客户端上,我使用JSON.Parse将此字符串解析为json结构。

planetsData = JSON.parse(data.planets);

for(var p in planetsData){

    var playerName = p;
    alert(p.position) //says "undefined" 
    var planetColor = p.color;
    var planetName = p.name;

    var planet = new Planet();
}

我可以成功获取pp.colorp.name,但是由于某种原因,它表明p.positionundefined,因此我无法访问p.position.xp.position.y参数。

1 个答案:

答案 0 :(得分:4)

将此行function allTable(users) { var body = document.getElementsByTagName("body")[0]; var tbl = document.createElement("table"); var tblBody = document.createElement("tbody"); for (var i = 0; i < users.length + 1; i++) { var row = document.createElement("tr"); for (var j = 0; j < 6; j++) { var cell = document.createElement("td"); if (i === 0 && j === 0) { var textCell = document.createTextNode("User"); cell.appendChild(textCell); } else if ( i === 0 && j === 1){ var textCell = document.createTextNode("Surname"); cell.appendChild(textCell); } else if ( i === 0 && j === 2){ var textCell = document.createTextNode("Second Surname"); cell.appendChild(textCell); } else if ( i === 0 && j === 3){ var textCell = document.createTextNode("Age"); cell.appendChild(textCell); } else if ( i === 0 && j === 4){ var textCell = document.createTextNode("Transport"); cell.appendChild(textCell); } else if ( i === 0 && j === 5){ var textCell = document.createTextNode("Image"); cell.appendChild(textCell); } else if (i > 0 && j === 0) { var textCell = document.createTextNode(users[i-1].name); cell.appendChild(textCell); } else if (i > 0 && j === 1) { var textCell = document.createTextNode(users[i-1].surname); cell.appendChild(textCell); } else if (i > 0 && j === 2) { var textCell = document.createTextNode(users[i-1].surname2); cell.appendChild(textCell); } else if (i > 0 && j === 3) { var textCell = document.createTextNode(users[i-1].age); celda.appendChild(textoCelda); } else if (i > 0 && j === 4) { var textCell = document.createTextNode(users[i-1].nameTransport); cell.appendChild(textCell); } else if (i > 0 && j === 5) { var textCell = document.createElement("img"); textCell.src = users[i-1].url; cell.appendChild(textCell); } row.appendChild(cell); } tblBody.appendChild(row); } tabla.appendChild(tblBody); body.appendChild(tabla); tabla.setAttribute("border", "2"); } 更改为此:

alert(p.position)

还有其他用途。

还可以添加以下内容:

 alert(planetsData[p].position);

首先循环,现在使用代码本身:

p=planetsData[p];