ListView的JQuery移动解析JSON

ListView的JQuery移动解析JSON,jquery,json,listview,mobile,Jquery,Json,Listview,Mobile,这个问题被问了很多次,在过去的3天里,我经历了很多不同的“解决方案”,但没有一个我能去工作 我有一个巨大的JSON文件,大约150k个条目,我想在JQuery Mobile中作为ListVIew查看。(我将使用过滤器实际使用数据) 我想出的最好的办法就是这个 <!DOCTYPE html> <html> <head> <title>Test</title> <meta name="viewport" content="width=

这个问题被问了很多次,在过去的3天里,我经历了很多不同的“解决方案”,但没有一个我能去工作

我有一个巨大的JSON文件,大约150k个条目,我想在JQuery Mobile中作为ListVIew查看。(我将使用过滤器实际使用数据)

我想出的最好的办法就是这个

<!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 var json='[“城市1”、“城市2”、“城市3”]; //jQuery getJSON将执行此步骤 var data=$.parseJSON(json); //这是你的密码 $.each(数据、函数(索引、值){ $('#output')。children('ul')。append(''+value+'

'))。listview('refresh'); });
如果删除.listview('refresh'),那么所有三个JSON条目都列在同一listview字段中。我显然想把它们分开

有人能建议怎么做吗

谢谢


Tim

您缺少
li
元素

$.each(data, function (index, value) {
        $('#output').children('ul').append('<li><p>'+value+'</p></li>').listview('refresh');
});
$。每个(数据、函数(索引、值){
$('#output')。children('ul')。append('
  • '+value+'

  • '))。listview('refresh'); });
    您缺少
    li
    元素

    $.each(data, function (index, value) {
            $('#output').children('ul').append('<li><p>'+value+'</p></li>').listview('refresh');
    });
    
    $。每个(数据、函数(索引、值){
    $('#output')。children('ul')。append('
  • '+value+'

  • '))。listview('refresh'); });
    要先使用,您需要有合适的

    所以我猜你的变量

    var json = '["City1","City2","City3"]';
    
    应该看起来更像:

    var json = '{"city":"City1"},{"city":"City2"},{"city":"City2"}';
    
    然后,在将其转换为JSON之前,您最好先将其拆分

    var jsonSplit = json.split(',');
    
    并将数组中每个分离的部分转换为JSON

    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对象进行操作

    ,以便首先使用

    $.each(data, function (index, value) {
            $('#output').children('ul').append('<li><p>'+value+'</p></li>').listview('refresh');
    });
    
    所以我猜你的变量

    var json = '["City1","City2","City3"]';
    
    应该看起来更像:

    var json = '{"city":"City1"},{"city":"City2"},{"city":"City2"}';
    
    然后,在将其转换为JSON之前,您最好先将其拆分

    var jsonSplit = json.split(',');
    
    并将数组中每个分离的部分转换为JSON

    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对象进行操作{
    $.each(data, function (index, value) {
            $('#output').children('ul').append('<li><p>'+value+'</p></li>').listview('refresh');
    });
    
    $('#output')。children('ul')。append('
  • '+value+'

  • '))。listview('refresh'); }); 根据您在此处所做的操作删除Listview(刷新),您不需要每次刷新。如果您是通过php动态添加它。。。最后需要刷新

    $.each(data, function (index, value) {
            $('#output').children('ul').append('<li><p>'+value+'</p></li>');
    });
    
    $。每个(数据、函数(索引、值){
    $(“#output”).children('ul').append(“
  • ”+value+”

  • ”); });
    $。每个(数据、函数(索引、值){
    $('#output')。children('ul')。append('
  • '+value+'

  • '))。listview('refresh'); });
    根据您在此处所做的操作删除Listview(刷新),您不需要每次刷新。如果您是通过php动态添加它。。。最后需要刷新

    $.each(data, function (index, value) {
            $('#output').children('ul').append('<li><p>'+value+'</p></li>');
    });
    
    $。每个(数据、函数(索引、值){
    $(“#output”).children('ul').append(“
  • ”+value+”

  • ”); });