简体   繁体   English

如何在列表视图中显示JSON数据?

[英]how to show JSON data in the Listview?

How can I show json data in LIST view? 如何在LIST视图中显示json数据? At this point I can get to json data in to alert dialog. 此时,我可以进入json数据以进入警报对话框。

My json data: 我的json数据:

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

this code for parse json data 此代码用于解析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);
                            //  } 

                };

you can create a table dynamically by jquery and fill it with your data. 您可以通过jquery动态创建一个表,并用您的数据填充它。 try something like this: 尝试这样的事情:

 $(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")); }); 
 table { border: 1px solid black; } td { border: 1px solid black; } th { border: 1px solid black; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.0/jquery.min.js"></script> <div id="container"></div> 

You could use a template library like underscore or handlebars to create a template for your list. 您可以使用下划线或车把等模板库为列表创建模板。 Alternatively you could use a variable 'list' and concatenate all the results coming from your json. 或者,您可以使用变量“列表”并连接来自json的所有结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM