从 JSON 制作 javascript var(数组)
Make the javascript var (array) from JSON
在一个页面(API页面)上,我有一个这样的PHP数组:
$arr = array("headline1" =>
array("name" => "Headline 1",
"description" => "Description of headline 1",
"items" => array(
array("name"=>"John", "lastname"=>"Doe"),
array("name"=>"Peter", "lastname"=>"Pan")
)
),
"headline2" => array("name" => "Headline 2",
"description" => "Description of headline 2",
"items" => array(
array("name"=>"Alexander", "lastname"=>"Doe"),
array("name"=>"Steven", "lastname"=>"Smith")
)
),
"headline3" => array("name" => "Headline 3",
"description" => "Description of headline 3",
"items" => array(
array("name"=>"John", "lastname"=>"Doeh"),
array("name"=>"Peter", "lastname"=>"Pans")
)
)
);
从这个数组中,脚本创建 json 编码版本:
echo json_encode($arr);
所以,我的问题是其他页面上的javascript。我想使用 jQuery $.getJSON 函数来检索这些数据并制作像这样的变量:
var categoryData = {
headline1: {
name: "Headline 1",
description: "Description of headline 1",
items: [
{
name: "John",
lastname: "Doe"
},
{
name: "Peter",
horoscope: "Pan"
}
]
},
headline2: {
name: "Headline 2",
description: "Description of headline 2",
items: [
{
name: "Alexander",
lastname: "Doe"
},
{
name: "Steven",
horoscope: "Smith"
}
]
},
headline3: {
name: "Headline 3",
description: "Description of headline 3",
items: [
{
name: "John",
lastname: "Doeh"
},
{
name: "Peter",
horoscope: "Pans"
}
]
}
};
如何使用jQuery $.getJSON函数实现这一点?
编辑:
我的其他Javascript函数(jQuery移动函数)
function showCategory( urlObj, options )
{
var categoryData = [];
$.getJSON('http://localhost/tst/sig.php', function(json) {
categoryData = json;
});
var categoryName = urlObj.hash.replace( /.*category=/, "" ),
// Get the object that represents the category we
// are interested in. Note, that at this point we could
// instead fire off an ajax request to fetch the data, but
// for the purposes of this sample, it's already in memory.
category = categoryData[ categoryName ],
// The pages we use to display our content are already in
// the DOM. The id of the page we are going to write our
// content into is specified in the hash before the '?'.
pageSelector = urlObj.hash.replace( /'?.*$/, "" );
if ( category ) {
// Get the page we are going to dump our content into.
var $page = $( pageSelector ),
// Get the header for the page.
$header = $page.children( ":jqmData(role=header)" ),
// Get the content area element for the page.
$content = $page.children( ":jqmData(role=content)" ),
// The markup we are going to inject into the content
// area of the page.
markup = "<p>" + category.description + "</p>",
// The array of items for this category.
cItems = category.items,
// The number of items in the category.
numItems = cItems.length;
// Generate a list item for each item in the category
// and add it to our markup.
for ( var i = 0; i < numItems; i++ ) {
markup += "<div data-role='collapsible' data-theme='a' data-content-theme='e'><h3>" + cItems[i].name + "</h3><p>"+ cItems[i].horoscope +"</p></div>";
}
markup += "</ul><p><a href='#one' data-rel='back' data-role='button' data-inline='true' data-icon='back'>Vratite se nazad</a></p> ";
// Find the h1 element in our header and inject the name of
// the category into it.
$header.find( "h1" ).html( category.name );
// Inject the category items markup into the content element.
$content.html( markup );
// Pages are lazily enhanced. We call page() on the page
// element to make sure it is always enhanced before we
// attempt to enhance the listview markup we just injected.
// Subsequent calls to page() are ignored since a page/widget
// can only be enhanced once.
$page.page();
// Enhance the listview we just injected.
$content.find( ":jqmData(role=collapsible)" ).collapsible();
$content.find( ":jqmData(role=button)" ).button();
// We don't want the data-url of the page we just modified
// to be the url that shows up in the browser's location field,
// so set the dataUrl option to the URL for the category
// we just loaded.
options.dataUrl = urlObj.href;
// Now call changePage() and tell it to switch to
// the page we just modified.
$.mobile.changePage( $page, options );
}
}
jQuery.getJSON
是异步的,需要一个回调函数,你不能直接将结果分配给变量。要使用它,您需要使PHP脚本的输出成为单个JSON字符串 - 在"对象文字"之前没有" var catData =
",后面没有分号。另外,不要忘记使用适当的MIME类型提供JSON。
相关文章:
- Var工作,但在使用数组执行相同操作时停止
- 从 JSON 制作 javascript var(数组)
- 如何在 JavaScript 中将数组中的 [ ] 替换为 var 结果
- for(数组中的i)和for(var i=0;i<array.length;i++)之间的差异
- 将json数组转换为node.jsexpress.js中的var值
- 剪切数组值并用 var i 填充它
- 将输入文件(多个文件)保存到数组 var js,jquery 中
- 如何在此 var 声明中获取此数组的第一个元素
- JavaScript 神奇地将空 var 插入到新数组中/更改其长度
- 从数组的元素赋值 var
- 如何将 var:var 推送到数组
- Why for var在数组中返回一个字符串索引
- 如何在客户端的nodejs中评估var数组
- Javascript:for(var i<array.length)v.s.for(数组中的e)
- 如何在mongodb中查询id存储在$in的“var-ids”数组中的文档
- 将Multiple Var输入函数,搜索JSON数组,并将Multiple Array Val作为输出接收
- javascript如何将数组中的每个元素传递给不同的var
- 如何将$_SESSION['var']数组传递给jquery并执行ajax
- 如何使用Javascript和Ajax传递一个HTML选择标签到PHP $_POST与一个(数组)var当名称属性是一个
- 将json输出分配给数组var