1

次のような JSON データがあります。

var data = '{"credit":{"@code":"NT2C8FFC","card":"NT2C8FFC","credit":"149.96","amount":"149.96","disabled":"0","expired":"2011-02-15 10:21:18","user":"xxxx","issued":"2010-02-15 10:21:18","orderid":[],"fromorder":[],"email":"xxxx","phone":[],"state":[],"address":[],"city":[],"zip":[],"country":[],"customerid":"xxx","order":"xxx","order_date":"2010-01-23 00:00:00","reason":"Product Not Working as Expected","source":"xxx","first":[],"last":[],"notes":[]}}'

実際にはJSONとして返されています。ここでは例としてこのように表示しています。data["credit"]呼び出された要素を取得するために使用できますcreditが、それは私だけを取得します:

{"@code":"NT2C8FFC","card":"NT2C8FFC","credit":"149.96","amount":"149.96","disabled":"0","expired":"2011-02-15 10:21:18","user":"xxxx","issued":"2010-02-15 10:21:18","orderid":[],"fromorder":[],"email":"xxxx","phone":[],"state":[],"address":[],"city":[],"zip":[],"country":[],"customerid":"xxx","order":"xxx","order_date":"2010-01-23 00:00:00","reason":"Product Not Working as Expected","source":"xxx","first":[],"last":[],"notes":[]}

1 つのショットにドリルダウンするにはどうすればよいですか? data["credit"]["credit"]は適切な構文ではありません。未定義になります。近くにいることは知っていますが、残りは思い出せません。目標は149.96、JSON データから取得することです。

4

3 に答える 3

1

今のところ、使用できますalert(data.credit.credit)

        var data = '{"credit":{"@code":"NT2C8FFC","card":"NT2C8FFC","credit":"149.96","amount":"149.96","disabled":"0","expired":"2011-02-15 10:21:18","user":"xxxx","issued":"2010-02-15 10:21:18","orderid":[],"fromorder":[],"email":"xxxx","phone":[],"state":[],"address":[],"city":[],"zip":[],"country":[],"customerid":"xxx","order":"xxx","order_date":"2010-01-23 00:00:00","reason":"Product Not Working as Expected","source":"xxx","first":[],"last":[],"notes":[]}}';
        eval("data=" + data);
        alert(data.credit.credit);

data = JSON.parse(data);を使用して文字列を Json に変換することもできますが、これにはブラウザーの互換性の問題があります。

対応ブラウザはこちら

  • Firefox (モジラ) 3.5
  • インターネット エクスプローラー 8
  • クロム
  • オペラ10
  • リスト項目
  • サファリ4

古いバージョンはサポートしていません。

于 2013-04-10T07:18:11.150 に答える
0

演算子を使用.してオブジェクトのオブジェクトを取得します

alert(data.credit.credit) //gives 149.96
 alert(data.credit.amount) //gives 149.96
alert(data.credit.card) //gives NT2C8FFC
于 2013-04-10T06:46:23.330 に答える