jQuery Mobile的解析JSON的ListView控件 [英] JQuery Mobile Parse JSON for ListView
问题描述
这个问题已经被问地段,花了过去三天通过一些不同的解决方案的打算,其中没有我可以开始工作。
This question has been asked lots and have spent the last 3 days going through a number of different 'solutions', none of which I can get to work.
我有一个巨大的JSON文件,有些项150K,那我要查看在jQuery Mobile的列表视图。 (我将使用过滤器实际使用中的数据)
I have a huge JSON file, some 150k entries, that I want to view as a ListVIew in JQuery Mobile. (I will be using the Filter to actually use the data)
我想出的最好的是这个
<!DOCTYPE html>
<html>
<head>
<title>Test</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="css/jqm-docs.css" />
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.0/jquery.mobile-1.0.min.css" />
<script type="text/javascript" src="http://code.jquery.com/jquery-1.6.4.min.js"></script>
<script type="text/javascript" src="http://code.jquery.com/mobile/1.0/jquery.mobile-1.0.min.js"></script>
</head>
<body>
<div data-role="page">
<div data-role="content">
<div id="output">
<ul data-role="listview" data-inset="true" data-filter="true">
</ul>
</div>
</div>
</div>
<script type="text/javascript">
//simulating the JSON coming from the server
var json = '["City1","City2","City3"]';
//jQuery getJSON will do this step
var data = $.parseJSON(json);
//and this is your code
$.each(data, function (index, value) {
$('#output').children('ul').append('<p>'+value+'</p>').listview('refresh');
});
</script>
</body>
</html>
如果我删除,然后所有三个JSON条目都在同一个ListView的字段中列出的.listview('刷新')。我当然希望他们分开。
If I remove the .listview('refresh') then all three JSON entries are listed in the same ListView field. I obviously want them seperated.
任何人都可以就如何做到这一点?
Can anyone advise on how to do this?
与感谢
添
推荐答案
为了使用 $ .parseJSON 首先你需要有适当的 JSON字符串格式
In order to use $.parseJSON first you need to have the proper JSON string format
所以我想你的变量
var json = '["City1","City2","City3"]';
应该看起来更像是:
should look more like:
var json = '{"city":"City1"},{"city":"City2"},{"city":"City2"}';
然后才将其转换为JSON,你宁愿要首先把它分解
then before to convert it to JSON, you would rather want to split it first
var jsonSplit = json.split(',');
和阵列内转换成JSON每一个分开的部分。
and convert to JSON every separated part within an array
var data = new Array(), i;
for(i in jsonSplit){
if(jsonSplit[i].length){ //remove last empty element after .split()
data[i] = $.parseJSON(jsonSplit[i]);
}
}
然后你可以操纵数据
作为一个JavaScript对象
then you can manipulate data
as a javascript object
这篇关于jQuery Mobile的解析JSON的ListView控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!