28

jQueryのループを使用してこのJSONを調べ、名前付きeachに追加しようとしています。JSONは次のとおりです。div#contentHere

{ "justIn": [
  { "textId": "123", "text": "Hello", "textType": "Greeting" },
  { "textId": "514", "text":"What's up?", "textType": "Question" },
  { "textId": "122", "text":"Come over here", "textType": "Order" }
  ],
 "recent": [
  { "textId": "1255", "text": "Hello", "textType": "Greeting" },
  { "textId": "6564", "text":"What's up?", "textType": "Question" },
  { "textId": "0192", "text":"Come over here", "textType": "Order" }
  ],
 "old": [
  { "textId": "5213", "text": "Hello", "textType": "Greeting" },
  { "textId": "9758", "text":"What's up?", "textType": "Question" },
  { "textId": "7655", "text":"Come over here", "textType": "Order" }
 ]
}

このコードを使用してこのJSONを取得しています:

$.get("data.php", function(data){

}) 

解決策はありますか?

4

4 に答える 4

56

試してみてください(テストされていません):

$.getJSON("data.php", function(data){
    $.each(data.justIn, function() {
        $.each(this, function(k, v) {
            alert(k + ' ' + v);
        });
    });
    $.each(data.recent, function() {
        $.each(this, function(k, v) {
            alert(k + ' ' + v);
        });
    });
    $.each(data.old, function() {
        $.each(this, function(k, v) {
            alert(k + ' ' + v);
        });
    });    
});

各データセットを異なる方法で処理する必要があるため、3つの別々のループ(justIn、recent、old)を考えました。そうでない場合は、次のことができます。

$.getJSON("data.php", function(data){
    $.each(data, function(k, v) {
        alert(k + ' ' + v);
        $.each(v, function(k1, v1) {
            alert(k1 + ' ' + v1);
        });
    });
}); 
于 2010-06-12T22:38:59.163 に答える
9

簡単なコードですが、フル機能です

以下は、各データの「レコード」をHTML要素にフォーマットし、データのプロパティをHTML属性値として使用するハイブリッドjQueryソリューションです。

jqueryeachは内部ループを実行します。for見出しとして表示するために(値ではなく)プロパティ名を取得できるようにするには、外側のループに通常のJavaScriptが必要でした。好みに応じて、わずかに異なる動作に変更できます。

これはコードの5つの主要な行だけですが、表示のために複数の行に折り返されています。

$.get("data.php", function(data){

    for (var propTitle in data) {

        $('<div></div>') 
            .addClass('heading')
            .insertBefore('#contentHere')
            .text(propTitle);

            $(data[propTitle]).each(function(iRec, oRec) {

                $('<div></div>')
                    .addClass(oRec.textType)
                    .attr('id', 'T'+oRec.textId)
                    .insertBefore('#contentHere')
                    .text(oRec.text);
            });
    }

});

出力を生成します

(注:「デバッグ」中に適切なレコードを適切な順序で表示するように、数字を前に付けてJSONデータのテキスト値を変更しました)

<div class="heading">
    justIn
</div>
<div id="T123" class="Greeting">
    1Hello
</div>
<div id="T514" class="Question">
    1What's up?
</div>
<div id="T122" class="Order">
    1Come over here
</div>
<div class="heading">
    recent
</div>
<div id="T1255" class="Greeting">
    2Hello
</div>
<div id="T6564" class="Question">
    2What's up?
</div>
<div id="T0192" class="Order">
    2Come over here
</div>
<div class="heading">
    old
</div>
<div id="T5213" class="Greeting">
    3Hello
</div>
<div id="T9758" class="Question">
    3What's up?
</div>
<div id="T7655" class="Order">
    3Come over here
</div>
<div id="contentHere"></div>

スタイルシートを適用する

<style>
.heading { font-size: 24px; text-decoration:underline }
.Greeting { color: green; }
.Question { color: blue; }
.Order { color: red; }
</style>

「美しく」見えるデータセットを取得するには

代替テキスト

詳細情報
JSONデータは次のように使用されました。

カテゴリごとに(アレイが保持されているキー名):

  • キー名はセクション見出しとして使用されます(例:justIn

配列内に保持されているオブジェクトごとに:

  • 「テキスト」はdivのコンテンツになります
  • 'textType'はdivのクラスになります(スタイルシートにフックされます)
  • 'textId'はdivのIDになります
  • 例:<div id = "T122"class="Order">ここに来てください</div>
于 2010-06-13T01:49:12.420 に答える
5

これは私のために働きます:

$.get("data.php", function(data){
    var expected = ['justIn', 'recent', 'old'];
    var outString = '';
    $.each(expected, function(i, val){
        var contentArray = data[val];
        outString += '<ul><li><b>' + val + '</b>: ';
        $.each(contentArray, function(i1, val2){
            var textID = val2.textId;
            var text = val2.text;
            var textType = val2.textType;
            outString += '<br />('+textID+') '+'<i>'+text+'</i> '+textType;
        });
        outString += '</li></ul>';
    });
    $('#contentHere').append(outString);
}, 'json');

これにより、次の出力が生成されます。

<div id="contentHere"><ul>
<li><b>justIn</b>:
<br />
(123) <i>Hello</i> Greeting<br>
(514) <i>What's up?</i> Question<br>
(122) <i>Come over here</i> Order</li>
</ul><ul>
<li><b>recent</b>:
<br />
(1255) <i>Hello</i> Greeting<br>
(6564) <i>What's up?</i> Question<br>
(0192) <i>Come over here</i> Order</li>
</ul><ul>
<li><b>old</b>:
<br />
(5213) <i>Hello</i> Greeting<br>
(9758) <i>What's up?</i> Question<br>
(7655) <i>Come over here</i> Order</li>
</ul></div>

そしてこのように見えます:

  • justIn
    (123)こんにちは挨拶
    (514)どうしたの?質問
    (122)ここに来て注文
  • 最近
    (1255)こんにちは挨拶
    (6564)どうしたの?質問
    (0192)ここに来て注文
  • 古い
    (5213)こんにちは挨拶
    (9758)どうしたの?質問
    (7655)ここに来て注文

contentTypeまた、として設定することを忘れないでください'json'

于 2010-06-12T22:43:29.370 に答える
0

私自身のサイトの1つでの私のソリューション、表付き:

$.getJSON("sections/view_numbers_update.php", function(data) {
 $.each(data, function(index, objNumber) {
  $('#tr_' + objNumber.intID).find("td").eq(3).html(objNumber.datLastCalled);
  $('#tr_' + objNumber.intID).find("td").eq(4).html(objNumber.strStatus);
  $('#tr_' + objNumber.intID).find("td").eq(5).html(objNumber.intDuration);
  $('#tr_' + objNumber.intID).find("td").eq(6).html(objNumber.blnWasHuman);
 });
});

section/view_numbers_update.php次のようなものを返します。

[{"intID":"19","datLastCalled":"Thu, 10 Jan 13 08:52:20 +0000","strStatus":"Completed","intDuration":"0:04 secs","blnWasHuman":"Yes","datModified":1357807940},
{"intID":"22","datLastCalled":"Thu, 10 Jan 13 08:54:43 +0000","strStatus":"Completed","intDuration":"0:00 secs","blnWasHuman":"Yes","datModified":1357808079}]

HTMLテーブル:

<table id="table_numbers">
 <tr>
  <th>[...]</th>
  <th>[...]</th>
  <th>[...]</th>
  <th>Last Call</th>
  <th>Status</th>
  <th>Duration</th>
  <th>Human?</th>
  <th>[...]</th>
 </tr>
 <tr id="tr_123456">
  [...]
 </tr>
</table>

これにより、サーバースクリプト時に、他の番号付き要素IDを許可するために、基本的にすべての行の前に「tr_」が付いた一意のIDが与えられます。次に、jQueryスクリプトはこのTR_ [id]要素を取得し、正しいインデックス付きセルにjsonリターンを入力します。

利点は、DBから完全な配列を取得でき、foreach($ array as $ record)でテーブルhtmlを作成するか、(更新要求がある場合)表示する前にdie(json_encode($ array))できることです。表はすべて同じページにありますが、表示コードは同じです。

于 2013-01-10T13:23:34.180 に答える