2010-12-28 22 views
0

最近Jquery.parseJsonについてここに投稿しました。今私は私の成果の一部です。私はこのスクリプトをstackoverflowユーザーのヘルプで動作させています。Jquery.parseJson ...パートIIを解析しようとしています

<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>'); 
     }); 
    }); 

PHP変数$データはJSONを返します。

これを動作させる方法の手掛かりはありますか? JSON形式の問題ですか?この質問の延長には申し訳ありません。

よろしく、

答えて

0

これは本当にあなたの質問への答えが、単なる提案ではありません。あなたはjson2ライブラリを試しましたか?それは本当に良いですし、JSONの解析とシリアライズを処理します。ここにリンクjson.orgがあります。縮小された場合、それは2.5kだから、それほど多くの追加のオーバーヘッドはありません。

0
 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> 

+0

私はAntonlaveyしかし、私は二番目。 – ernix

+0

こんにちは、返信いただきありがとうございます。しかし、それは動作していません。たぶんJSONは適切な方法で生成されませんか? –

0
var json = eval(<?php echo $data; ?>); // Will not work 

eval関数を取り除きます。 JSONは、このコンテキストで、サーバー生成のJavaScriptコードから変数に割り当てることで有効なJavaScriptです。そして、それが正常に動作する必要があります:

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

あなたはしかし、この行を修正する必要があります:

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

json[1]["1"]代わりのjson.1ようなものを試してみてください。

関連する問題