【问题标题】:Trying to parse with Jquery.parseJson... part II试图用 Jquery.parseJson 解析...第二部分
【发布时间】:2010-12-28 17:27:17
【问题描述】:

我最近在这里发布了有关 Jquery.parseJson 的信息。现在我进入了我成就的第二部分。我让这个脚本与 stackoverflow 用户帮助一起工作。

<input type="text" id="query" /><button>search</button><br />
<div id="results">

</div>

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
    $(document).ready(function(){
        $('button').click(function(){
            $("#query").val(); // Do nothing with this value
            var json = eval('({"name":"John"})');
            $("#results").append('<p>' + json.name + '</p>');
            // $.getJSON('{"name":"John"}',function(json){
            //     $.each(json.results,function(i,obj){
            //         $("#results").append('<p>' + obj.name + '</p>');
            //     });
            // });
        });
    });

现在我的代码已经工作了,我已经构建了一个真正的 JSON 来解析,这里是:

["http:\/\/guimaraes-braga.olx.pt\/seat-leon-1-9tdi-sport-111cv-iid-148286318",
 {"1":"http:\/\/static04.olx-st.com\/images\/v4\/logos\/logo-default.png"},
     {"2":"http:\/\/images03.olx.pt\/ui\/10\/71\/18\/t_1292857323_148286318_1.jpg"},
 {"3":"http:\/\/images04.olx.pt\/ui\/10\/71\/18\/t_1292886514_148286318_2.jpg"},
        {"4":"http:\/\/images04.olx.pt\/ui\/10\/71\/18\/t_1292886514_148286318_3.jpg"},
 {"5":"http:\/\/images02.olx.pt\/ui\/10\/71\/18\/t_1292886514_148286318_4.jpg"},
 {"6":"http:\/\/images02.olx.pt\/ui\/10\/71\/18\/t_1292886514_148286318_5.jpg"},
 {"7":"http:\/\/images02.olx.pt\/ui\/10\/71\/18\/t_1292886514_148286318_6.jpg"},
 {"8":"http:\/\/images03.olx.pt\/ui\/10\/71\/18\/t_1292857323_148286318_1.jpg"},
 {"9":"http:\/\/images04.olx.pt\/ui\/10\/71\/18\/t_1292886514_148286318_2.jpg"},
 {"10":"http:\/\/images04.olx.pt\/ui\/10\/71\/18\/t_1292886514_148286318_3.jpg"},
 {"11":"http:\/\/images02.olx.pt\/ui\/10\/71\/18\/t_1292886514_148286318_4.jpg"},
 {"12":"http:\/\/images02.olx.pt\/ui\/10\/71\/18\/t_1292886514_148286318_5.jpg"},
 {"13":"http:\/\/images02.olx.pt\/ui\/10\/71\/18\/t_1292886514_148286318_6.jpg"},
 {"14":"http:\/\/images01.olx.com\/images\/spinner.gif"},
 {"15":"http:\/\/images01.olx.pt\/ui\/10\/71\/18\/1292886514_148286318_1-Fotos-de--SEAT-LEON-19TDI-SPORT-111CV.jpg"},
 {"16":""}]

上面的这个 JSON 包含图像的 URL,我尝试像在第一个脚本中一样传递这个 JSON,但是它不起作用……下面的代码中有更多细节。

<?php $json = new img_json_output(); ?>
<?php $data = $json->get_img_tags_as_json($json->get_page('http://guimaraes-braga.olx.pt/seat-leon-1-9tdi-sport-111cv-iid-148286318')); ?>

<input type="text" id="query" /><button>search</button><br />
<div id="results">

</div>

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
    $(document).ready(function(){
        $('button').click(function(){
            $("#query").val(); // Do nothing with this value
            var json = eval(<?php echo $data; ?>);
            $("#results").append('<p>' + json.1 + '</p>');
        });
    });
</script>

PHP 变量 $data 返回 JSON。

如何让它工作?是我的 JSON 格式有问题吗?

【问题讨论】:

    标签: jquery json parsing


    【解决方案1】:

    这并不是对您问题的真正答案,而只是一个建议。你试过 json2 库吗?它非常好,可以处理 json 的解析和序列化。这是一个链接json.org。缩小后为 2.5k,因此没有太多额外开销。

    【讨论】:

      【解决方案2】:
              var json = eval( "(" + '<?php echo $data; ?>' + ")" );
      

      尝试检查:
      1. JSON 字符串不包含回车。
      2.方法链。最外层的结构是一个数组。它应该是 json[0] 或 json[1]["1"]。

      以下脚本适用于我。

       <body>
           <input type="text" id="query" /><button>search</button><br />
           <div id="results">
      
           </div>
      
           <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js" type="text/javascript"></script>
           <script type="text/javascript">
               $(document).ready(function(){
                   $('button').click(function(){
                       var json = eval('(' + '["http:\/\/guimaraes-braga.olx.pt\/seat-leon-1-9tdi-sport-111cv-iid-148286318", {"1":"http:\/\/static04.olx-st.com\/images\/v4\/logos\/logo-default.png"}, {"2":"http:\/\/images03.olx.pt\/ui\/10\/71\/18\/t_1292857323_148286318_1.jpg"}, {"3":"http:\/\/images04.olx.pt\/ui\/10\/71\/18\/t_1292886514_148286318_2.jpg"}, {"4":"http:\/\/images04.olx.pt\/ui\/10\/71\/18\/t_1292886514_148286318_3.jpg"}, {"5":"http:\/\/images02.olx.pt\/ui\/10\/71\/18\/t_1292886514_148286318_4.jpg"}, {"6":"http:\/\/images02.olx.pt\/ui\/10\/71\/18\/t_1292886514_148286318_5.jpg"}, {"7":"http:\/\/images02.olx.pt\/ui\/10\/71\/18\/t_1292886514_148286318_6.jpg"}, {"8":"http:\/\/images03.olx.pt\/ui\/10\/71\/18\/t_1292857323_148286318_1.jpg"}, {"9":"http:\/\/images04.olx.pt\/ui\/10\/71\/18\/t_1292886514_148286318_2.jpg"}, {"10":"http:\/\/images04.olx.pt\/ui\/10\/71\/18\/t_1292886514_148286318_3.jpg"}, {"11":"http:\/\/images02.olx.pt\/ui\/10\/71\/18\/t_1292886514_148286318_4.jpg"}, {"12":"http:\/\/images02.olx.pt\/ui\/10\/71\/18\/t_1292886514_148286318_5.jpg"}, {"13":"http:\/\/images02.olx.pt\/ui\/10\/71\/18\/t_1292886514_148286318_6.jpg"}, {"14":"http:\/\/images01.olx.com\/images\/spinner.gif"}, {"15":"http:\/\/images01.olx.pt\/ui\/10\/71\/18\/1292886514_148286318_1-Fotos-de--SEAT-LEON-19TDI-SPORT-111CV.jpg"}, {"16":""}]' + ')');
                       for (var i=1; i<json.length; i++){
                           $("#results").append('<p>' + json[i][i] + '</p>');
                       }
                   });
               });
           </script>
          <hr />
       </body>
      

      【讨论】:

      • 您好,感谢您的回复。但它不起作用。也许不是以正确方式生成的 JSON?
      【解决方案3】:
      var json = eval(<?php echo $data; ?>); // Will not work
      

      摆脱 eval 函数。在将它分配给来自服务器生成的 JavaScript 代码的变量的上下文中,JSON 是有效的 JavaScript。那么它应该可以正常工作:

      var json = <?php echo $data; ?>; // Works fine
      

      您确实需要修复此行:

      $("#results").append('<p>' + json.1 + '</p>');
      

      尝试使用 json[1]["1"] 代替 json.1

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-05-12
        • 2011-12-01
        • 1970-01-01
        • 1970-01-01
        • 2018-10-31
        • 2012-03-16
        相关资源
        最近更新 更多