How can I show json data in LIST view? At this point I can get to json data in to alert dialog.
My json data:
[{"_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
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. 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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.