1

ここではJavascriptを初めて使用します。JQueryを使用してJSONを解析するのに少し問題があります。これは私のJSONの構造にかかっていると思いますが、これらのスクリプトを別のサービスに使用しているので、そこで構造を変更する必要はありません。

私のJSONは次のとおりです

{"customerInAccount":[{"customer_name":"Jane Doe","auto_id":"21"},{"customer_name":"Jack   Black","auto_id":"22"}]}

理想的には、customer_nameを使用してリストビューにデータを入力し、auto_idをどこかに(おそらくローカルストレージ)保存して、選択した場合に関連データを表示できるようにします。

誰かが私がこのJSONを解析するためのループを構築するのを手伝ってくれるなら、私は本当にそれをいただければ幸いです。

4

3 に答える 3

3

jQueryを使用して簡単に解析できます。

$.parseJSON(data); 

これを試して:

var json = $.parseJSON(data);
for(var i=0; i<json.customerInAccount.length; i++){
     // do your stuff here..
}

問題が発生した場合はお知らせください

于 2013-02-28T11:53:45.397 に答える
1

このように使用する

var value=yourJsonString
var results=JSON.parse(value); 

$.each(results.customerInAccount,function(i,item){
 // do with item 
alert(  item.customer_name);
});

作業デモ:ループを使用したJSON解析

于 2013-02-28T11:53:59.513 に答える
1

あなたは次のようにすることができます。

$('#home').live('pageshow', function(){
    // creating object 
    var customerList = JSON.parse('{"customerInAccount":[{"customer_name":"Jane Doe","auto_id":"21"},{"customer_name":"Jack   Black","auto_id":"22"}]}');

    // creating html string
    var listString = '<ul data-role="listview" id="customerList">';

    // running a loop
    $.each(customerList.customerInAccount, function(index,value){
     listString += '<li><a href="#" data-cusid='+this.auto_id+'>'+this.customer_name+'</a></li>';
    });
    listString +='</ul>';

    console.log(customerList);

    //appending to the div
    $('#CustomerListDiv').html(listString);

    // refreshing the list to apply styles
    $('#CustomerListDiv ul').listview();

    // getting the customer id on the click
    $('#customerList a').bind('click',function(){
     var customerID = $(this).data('cusid');  
     alert(customerID);
    });
});

ここでアクティブなフィドルをチェックできます。http://jsfiddle.net/amEge/3/

于 2013-02-28T13:17:48.213 に答える