admin管理员组文章数量:1336142
This is the JSON I'm trying to create programmatically:
{
"sensors": [{
"x": 342,
"y": 213,
"id": 4
}, {
"x": 642,
"y": 913,
"id": 3
}, {
"x": 452,
"y": 113,
"id": 2
}]
}
I have to iterate through all elements with a specific class to retrieve the data for the json:
$(".marker").each(function()
{
var x = $(this).attr("data-x");
var y = $(this).attr("data-y");
var id = $(this).attr("data-id");
});
How could I create the json object to be like I described?
This is the JSON I'm trying to create programmatically:
{
"sensors": [{
"x": 342,
"y": 213,
"id": 4
}, {
"x": 642,
"y": 913,
"id": 3
}, {
"x": 452,
"y": 113,
"id": 2
}]
}
I have to iterate through all elements with a specific class to retrieve the data for the json:
$(".marker").each(function()
{
var x = $(this).attr("data-x");
var y = $(this).attr("data-y");
var id = $(this).attr("data-id");
});
How could I create the json object to be like I described?
Share Improve this question asked Aug 12, 2013 at 11:06 CornwellCornwell 3,4108 gold badges54 silver badges85 bronze badges2 Answers
Reset to default 11Try this
var json = {"sensors":[]};
$(".marker").each(function()
{
var x = $(this).attr("data-x");
var y = $(this).attr("data-y");
var id = $(this).attr("data-id");
json.sensors.push({"x":x, "y":y,"id":id});
});
Look JSON.parse method on MDN ou MSDN
var jsontext = '{"firstname":"Jesper","surname":"Aaberg","phone":["555-0100","555-0120"]}';
var contact = JSON.parse(jsontext);
document.write(contact.surname + ", " + contact.firstname);
// Output: Aaberg, Jesper
本文标签: javascriptProgrammatically create jsonStack Overflow
版权声明:本文标题:javascript - Programmatically create json - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742395506a2466839.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论