0

我需要用 javascript 解析这个 json

    {
     "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"
      }
    ]
  }
}

当这段代码只有一个结果时,我已经设法做到了

 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);
}

我把我的答案写成一个循环,因为我假设你想处理所有的产品。如果您只想访问第一个产品,则可以访问 products 数组:

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]

要获得该元素的属性之一,您可以再次使用点符号 -

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-language-workbench

规则语法文档: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 question 和http://en.wikipedia.org/wiki/JSON上的第一个输入。

于 2012-10-26T21:29:44.083 回答