0

このjsonをjavascriptで解析する必要があります

    {
     "MasterProducts": {
    "MasterProduct": [
      {
        "Productcode": "0903-000192",
        "Description": "ICROCOMPUTER;32FX200 24BIT QFP 132P",
        "ThumbPic": "NoImage.png",
        "RRP": "41.400000",
        "Stock": "0"
      },
      {
        "Productcode": "0160-030",
        "Description": "AXIS MPEG-4 Decoder 50-user licence pack onto 50 separate computers. For all Axis MPEG-4 products that do not support AAC audio encoding. (210 211 210A 211A 213 214 221 225FD 231D+ 232D+ 241S 241Q 241SA 241QA 242SIV 243SA 282 282A).",
        "ThumbPic": "NoImage.png",
        "RRP": "35.230000",
        "Stock": "0"
      },
      {
        "Productcode": "0160-040",
        "Description": "AXIS MPEG-4 +ACC Decoder 50-user license onto 50 separate computers. For all Axis products that supports MPEG-4. (207 207W 207MW 212PTZ 216FD 223M).",
        "ThumbPic": "NoImage.png",
        "RRP": "50.880000",
        "Stock": "0"
      },
      {
        "Productcode": "10403E",
        "Description": "Hotsync palm computer cradle/docking station",
        "ThumbPic": "NoImage.png",
        "RRP": "0.000000",
        "Stock": "2"
      },
      {
        "Productcode": "0903-000193",
        "Description": "ICROCOMPUTER;32FX200 24BIT QFP 132P",
        "ThumbPic": "NoImage.png",
        "RRP": "37.790000",
        "Stock": "0"
      }
    ]
  }
}

このコードで結果が1つだけの場合、私はそれを行うことができました

 if(data !== '')
    {
        xmlData = data.childNodes[0].childNodes[0].data;
        objData = app.XML2OBJ(xmlData);            
        var item = objData.MasterProduct.Description;
        //alert(item);
    }

ただし、複数の結果セットで機能させることはできません。

4

3 に答える 3

1

解析する必要はありません。JSON は JavaScript オブジェクトの作成方法です。

// alerting each product description:
for(var product in data.MasterProducts.MasterProduct){
    // "product" is the current iteration in the products array
    alert(product.Description);
    // You can access all the product properties here.
    alert(product.Productcode);
}

それはそれを行う必要があります。ここで、マスター製品が 1 つだけ返される可能性があるとします。その場合、MasterProducts プロパティは配列ではなくオブジェクトであり、上記の for ループはおそらく壊れます。これを防ぐ最も簡単な方法は、ループする前に MasterProducts を空の配列に連結することです。わずかなパフォーマンスのペナルティがあるかもしれませんが、それほど重要ではないと思います.

// Concat to empty array first
data.MasterProducts.MasterProduct = [].concat(data.MasterProducts.MasterProduct);
// alerting each product description:
for(var product in data.MasterProducts.MasterProduct){
    // "product" is the current iteration in the products array
    alert(product.Description);
    // You can access all the product properties here.
    alert(product.Productcode);
}

すべての製品を処理したいと思うので、回答をループとして書きました。最初の製品にアクセスしたいだけの場合は、製品配列にアクセスできます。

data.MasterProducts.MasterProduct[0].Description
于 2012-10-26T09:19:10.193 に答える
0

上記のコメントで議論されているように、通常のバニラ JavaScript を使用してデータにアクセスするだけです。

var data = // JSON 

console.log(data.MasterProducts.MasterProduct[0].Productcode);

を与えます0903-000192。角括弧を含むオブジェクト[]は配列です。それらについては、 の配列表記を使用できますarr[0]。オブジェクトの場合、そのキー名を使用できます。

{
"MasterProducts": {
 "MasterProduct": [
   {
     "Productcode": "0903-000192",
     "Description": "ICROCOMPUTER;32FX200 24BIT QFP 132P",
     "ThumbPic": "NoImage.png",
     "RRP": "41.400000",
     "Stock": "0"
   }
 ]
}

MasterProductsはルート オブジェクトなので、エントリ ポイントとして使用できます。

var products = data.MasterProducts;

ここで、製品のリストの最初の要素を取得するには、配列表記を使用できます。

products.MasterProduct[0]

その要素のプロパティの 1 つを取得するには、再びドット表記を使用できます -

products.MasterProduct[0].Description //"ICROCOMPUTER;32FX200 24BIT QFP 132P"
于 2012-10-26T09:19:30.297 に答える
0

json は JavaScript にネイティブです - パーサーは必要ありません。

しかし、NPEG を使用して、json を含むあらゆるタイプのドキュメントを解析する方法を紹介します。

JavaScript を使用したドキュメントの解析

https://github.com/leblancmeneses/NPEG/tree/master/Languages/npeg_javascriptを使用することもでき ます

C バージョンをエクスポートできるビジュアル ツールは次のとおりです

ルール文法のドキュメント: http://www.robusthaven.com/blog/parsing-expression-grammar/npeg-dsl-documentation

ルール

    S: [\s]*;
(?<StringValue>):  '"' (?<String> [^"]+) '"' / "'" (?<String> [^']+) "'";
(?<NumberValue>):  [0-9]+ ('.' [0-9]+)?;

(?<KeyValuePair>): ( '"'  (?<Key>[^"]+) '"' / "'" (?<Key> [^']+) "'" ) S ':' S  (StringValue / NumberValue / ValueRecursive) S;
(?<Object>): '{' S ( S  KeyValuePair S  ','? )*  S '}'; 
(?<Array>): '[' S (  S Object S  ','? )* S ']';
ValueRecursive: Array / Object ;
(?<JSON>): S ValueRecursive S;

試した入力: この SO の質問とhttp://en.wikipedia.org/wiki/JSONでの最初の入力。

于 2012-10-26T21:29:44.083 に答える