0

我有一个数组:

var sampleitems =[];

我从返回值的 url 获取 json 数据

 WinJS.xhr({
        type: "GET",
        url: "http://some_webpage.php", 

    }).then(function (success) { 
// DYNAMICALLY PUSH CONTENTS TO ARRAY 'sampleitems'
    }, 
  function (error) {
      document.getElementById("check").innerText = "vada pochu";
  }

我需要数组看起来像:

var sampleitems = [
{ key: "group1", title: "Group Title: 1", subtitle: "Group Subtitle: 1", backgroundImage: darkGray, description: groupDescription } ];

如何动态地向示例项添加值以及我的 PHP 页面应如何以哪种格式返回 JSON?

4

3 回答 3

0

您应该查看 PHP 的JSON_ENCODE() 函数。下面的代码应该输出您需要的 JSON(我没有检查它是否有效)。

$sampleitems=array(
 array(
  'key'=>'group1',
  'title'=>'Group Title: 1',
  ...
 )
);

echo json_encode($sampleitems);
于 2013-11-06T10:56:14.630 回答
0

sampleitems.push({ key: "group1", title: "Group Title: 1", subtitle: "Group Subtitle: 1", backgroundImage: darkGray, description: groupDescription });

于 2013-11-06T10:56:21.887 回答
0

使数组执行 json_encode,如:

$sampleitems = array(
        'key' => 'group1',
        'title' => 'Group Title: 1',
        'subtitle' => 'Group Subtitle: 1',
        'backgroundImage' => 'darkGray',
        'description' => 'groupDescription'
    );
    echo '['.json_encode($sampleitems).']';

结果:

[{"key":"group1","title":"Group Title: 1","subtitle":"Group Subtitle: 1","backgroundImage":"darkGray","description":"groupDescription"}]
于 2013-11-06T10:59:56.817 回答