2012-04-26 62 views
3

可能重複:
how to parse json in javascript解析JSON在JavaScript或jQuery的

我需要解析此JSON在JavaScript或jQuery的。請幫助我在下面的JSON中獲取產品列表。

要獲得產品列表

{ 
    "main": { 
     "ProductsData": { 
      "Product": { 
       "AdjustmentTypeID": "0", 
       "BrandID": "4", 
       "BrandName": "Joseph Joseph", 
       "ChildrenGenerated": "False", 
       "Cost": "8.50", 
       "Description": "<span style=\"line-height: 120%; \">The ingenious dual-chamber design of this measuring jug eliminates the need for separate measuring spoons, cups and jugs. Use the small chamber to accurately measure liquids from as little as a single teaspoon (5ml), and then for greater volumes (up to 550ml) simply turn the jug 180&ordm; and use the larger chamber. Made from SAN material. Heat resistant to 90&deg;C \\/ 190&deg;F.<\\/span>\\u000d\\u000a<p class=\"MsoNormal\" style=\"margin-bottom:0cm;margin-bottom:.0001pt;line-height:\\u000d\\u000a120%;mso-layout-grid-align:none;text-autospace:none;vertical-align:middle\"><br \\/>\\u000d\\u000aDesign registered<span lang=\"EN-US\"><o:p><\\/o:p><\\/span><\\/p>\\u000d\\u000a<p class=\"BasicParagraph\"><span style=\"font-size:11.0pt;line-height:120%;\\u000d\\u000afont-family:&quot;Calibri&quot;,&quot;sans-serif&quot;;color:windowtext\"><br \\/>\\u000d\\u000aDimensions&nbsp;&nbsp; 7x 7 x 15cm<o:p><\\/o:p><\\/span><\\/p>", 
       "DownloadFile": "", 
       "InternalCode": "502842009381 0", 
       "IsProductActive": "False", 
       "ManufacturerID": "11", 
       "ManufacturerName": "Joseph Joseph", 
       "OptionMatchGroupID": "", 
       "ParentProduct": "", 
       "ProductID": "80", 
       "ProductName": "2-in-1 Measuring Jug", 
       "ProductTypeDescription": "Compound Product", 
       "ProductTypeID": "8", 
       "SiteID": "57", 
       "StockLevel": "", 
       "SupplierID": "3", 
       "SupplierName": "Joseph Joseph", 
       "UseStockControl": "False", 
       "VatRate": "20" 
      } 
     } 
    } 
} 
+1

你甚至使用過Google嗎? – 2012-04-26 09:33:38

回答

2

在jQuery中使用parseJSON method

實施例:

var obj = $.parseJSON(yourJsonString); 
alert(obj.main.ProductsData.Product.Cost); 
0

您可以使用jQuery功能$.parseJSON(jsonString)獲得JSON對象

如果jsonString存儲您提供的字符串,

jsonObj = $.parseJSON(jsonString); 

jsonObj.main.ProductsData.Product

將是琳琅滿目的商品

1

只需使用JSON.parse功能(MDN docu link

yourObj = JSON.parse(jsonstring); 

之後,您可以通過您的訪問對象的JSON字符串的任何財產,因此,例如,

yourObj['main']['ProductsData']['ProductName'] 

將返回

"2-in-1 Measuring Jug" 

在你給出的例子。

0

使用JavaScript:JSON.parse(yourjavascriptobjecttoparse);

然後var productx = yourjavascriptobjecttoparse['main']['ProductsData']['Product']引用產物。

0

您可以使用

var object = $.parseJSON(jsonString); 

現在,您可以訪問該對象就像一個原生的JavaScript對象:從字符串

myobject = $.parseJSON("myfile.json") 

或閱讀JSON:

object.main.ProductsData.Product.BrandName  // Joseph Joseph 
2

閱讀JSON從文件:

myobject = $.parseJSON(jsonString) 

現在得到你想要的數據:

//Loops into every Product in ProductsData: 
$(myobject.main.ProductsData.Product).each(function(index, element){ 
    //Do something with Product variable such as below 
    alert(element.BrandName + ' ' + element.SupplierName); 
} 

請務必http://jsonlint.com/

+0

不,您不能將文件名發送到'parseJSON'方法。 – Guffa 2012-04-27 06:00:17

+0

@Guffa不,我們可以。我做了一次。你可以在這裏看到http://stackoverflow.com/questions/10082929/create-contact-table-from-json-data – Shinigamae 2012-04-27 13:25:25

0

檢查您的JSON數據有一個有趣的JSON的jQuery插件稱爲jquery-json

它允許您同時序列化和反序列化JSON。它的工作原理是這樣的:

var myObject = { property1: "value1", property2: "value2" }; 

// Converts myObject to JSON 
var serialized = $.toJSON(myObject); 

// Parses generated JSON into a new object 
var deserialized = $.evalJSON(serialized);