如何在Listview中显示JSON数据?

时间:2014-10-24 09:29:39

标签: javascript jquery json titanium-mobile

如何在LIST视图中显示json数据? 此时我可以使用json数据进入警报对话框。

我的json数据:

    [{"_id":"5449f20d88da65bb79a006e1","name":"name3","phone":"888888","service":"service5","amount":"66666 "},
     {"_id":"5449f20c88da65bb79a006e0","name":"name3","phone":"888888","service":"service5","amount":"66666 "}]

此代码用于解析json数据

var xhr = Titanium.Network.createHTTPClient();
                    xhr.setTimeout(10000);

                    xhr.open("GET","url");  
                    xhr.setRequestHeader("Content-Type", "application/json; charset=utf-8");
                    xhr.send();
                    //xhr.send("method=system.connect");
                    xhr.onerror = function() {

                        Titanium.API.info("Error in connecting to server !!");
                        };

                        xhr.onload = function() {

                        var responce = JSON.parse(this.responseText);
                        var name= responce[0].name;
                        var amount = responce[1].amount;
                        alert(name +'  '+ phone + ' ' + service + ' ' + amount);

                               //  var daysh = eval('(' + this.responseText + ')');
                             //   var status = daysh.status;
                             //   var jsonArry = daysh.locations;
                             //   for (var i = 0; i < jsonArry.length; i++)
                               // {                             
                            //    alert(i);
                            //  } 

                };

2 个答案:

答案 0 :(得分:2)

你可以通过jquery动态创建一个表,并用你的数据填充它。尝试这样的事情:

&#13;
&#13;
$(document).ready(function() {


  var response = [{
    "_id": "5449f20d88da65bb79a006e1",
    "name": "name3",
    "phone": "888888",
    "service": "service5",
    "amount": "66666 "
  }, {
    "_id": "5449f20c88da65bb79a006e0",
    "name": "name3",
    "phone": "888888",
    "service": "service5",
    "amount": "66666 "
  }];



  var table = $("<table>");
  table.append($("<tr><th>ID</th><th>Name</th><th>Phone</th><th>Service</th><th>Amount</th></tr>"))
  for (var i = 0; i < response.length; i++) {

    var row = $("<tr><td>" + response[i]['_id'] + "</td><td>" + response[i]['name'] + "</td><td>" + response[i]['phone'] + "</td><td>" + response[i]['service'] + "</td><td>" + response[i]['amount'] + "</td></tr>");
    table.append(row);
  }

  table.appendTo($("#container"));

});
&#13;
table {
  border: 1px solid black;
}
td {
  border: 1px solid black;
}

th {
  border: 1px solid black;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.0/jquery.min.js"></script>
<div id="container"></div>
&#13;
&#13;
&#13;

答案 1 :(得分:1)

您可以使用下划线或把手等模板库为列表创建模板。 或者,您可以使用变量'list'并连接来自json的所有结果。