0

ajaxを使用してphpファイルからjavascriptにデータを送信しています。このようなJavaScript変数を取得したい。

var members = [
    {memberId : 1, parentId:null, amount:200, otherInfo:"blah"},
    {memberId : 2, parentId:1, amount:300, otherInfo:"blah1"},
    {memberId : 3, parentId:1, amount:400, otherInfo:"blah2"},
    {memberId : 4, parentId:3, amount:500, otherInfo:"blah3"},
    {memberId : 6, parentId:1, amount:600, otherInfo:"blah4"},
    {memberId : 9, parentId:4, amount:700, otherInfo:"blah5"},
    {memberId : 12, parentId:2, amount:800, otherInfo:"blah6"},
    {memberId : 5, parentId:2, amount:900, otherInfo:"blah7"},
    {memberId : 13, parentId:2, amount:0, otherInfo:"blah8"},
    {memberId : 14, parentId:2, amount:800, otherInfo:"blah9"},
    {memberId : 55, parentId:2, amount:250, otherInfo:"blah10"},
    {memberId : 56, parentId:3, amount:10, otherInfo:"blah11"},
    {memberId : 57, parentId:3, amount:990, otherInfo:"blah12"},
    {memberId : 58, parentId:3, amount:400, otherInfo:"blah13"},
    {memberId : 59, parentId:6, amount:123, otherInfo:"blah14"},
    {memberId : 54, parentId:6, amount:321, otherInfo:"blah15"},
    {memberId : 53, parentId:56, amount:10000, otherInfo:"blah7"},
    {memberId : 52, parentId:2, amount:47, otherInfo:"blah17"},
    {memberId : 51, parentId:6, amount:534, otherInfo:"blah18"},
    {memberId : 50, parentId:9, amount:55943, otherInfo:"blah19"},
    {memberId : 22, parentId:9, amount:2, otherInfo:"blah27"},
    {memberId : 33, parentId:12, amount:-10, otherInfo:"blah677"}

];

jsonとは違う

[{"memberId":"4","parentId":"1","amount":"10","otherInfo":"sds"},{"memberId":"5","parentId":"1","amount":"100","otherInfo":"dsf"},{"memberId":"6","parentId":"4","amount":"1000","otherInfo":"sadsa"}]
4

3 に答える 3

0

jsonを読み取ってから配列を抽出してみてください。その前に、目的のフィールドを持つオブジェクトを作成する必要があるため、そのインスタンスを作成して配列に追加します

var items = new Array();
var jsonData = JSON.parse(jsoneString);
for (var i in jsonData) {
    var item = jsonData[i];
    var prop1 = item.memberId;
    //and other items goes here

}

これはあなたにとって適切な解決策だと思います。

于 2012-09-18T11:33:32.723 に答える
0

私はこのようなコードを書いて、正しく動作しました

response = [{"memberId":"4","parentId":"1","amount":"10","otherInfo":"sds"},{"memberId":"5","parentId":"1","amount":"100","otherInfo":"dsf"},{"memberId":"6","parentId":"4","amount":"1000","otherInfo":"sadsa"}];

response = jQuery.parseJSON(response);
var members = [
       {memberId : current_id , parentId:null, name:current_name}
    ];
for(var i=0; i<response.length;i++){
   user = {memberId: parseInt(response[i]['memberId']), parentId: parseInt(response[i]['parentId']), name: response[i]['name']};
members.push(user);
}
于 2012-09-19T10:15:41.093 に答える
0

このような:

<script>
var members = <?php echo json_encode ($members); ?>;
</script>

またはこれ:

<script src="source.php"></script>

またはこれ:

<script>
    jQuery.getJSON ( 'source.php' );
</script>

ソース.php:

var members = <? echo json_encode ( $members ); ?>
于 2012-09-18T11:27:09.653 に答える