Parse Json in javascript or jquery
本问题已经有最佳答案,请猛点这里访问。
Possible Duplicate:
how to parse json in javascript
我需要在javascript或jquery中解析这个JSON。请帮助我在下面的JSON中获取产品列表。
获取产品列表
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | { "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º and use the larger chamber. Made from SAN material. Heat resistant to 90°C \\/ 190°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:"Calibri","sans-serif";color:windowtext"><br \\/>\\u000d\\u000aDimensions 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" } } } } |
从文件读取json:
1 | myobject = $.parseJSON("myfile.json") |
或者从字符串中读取json:
1 | myobject = $.parseJSON(jsonString) |
现在获取所需的数据:
1 2 3 4 5 | //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查看您的JSON数据。/
在jquery中使用
例子:
1 2 | var obj = $.parseJSON(yourJsonString); alert(obj.main.ProductsData.Product.Cost); |
只需使用
1 | yourObj = JSON.parse( jsonstring ); |
然后,您可以通过对象访问JSON字符串的任何属性,例如,
1 | yourObj['main']['ProductsData']['ProductName'] |
将返回
1 | "2-in-1 Measuring Jug" |
在您的示例中。
jquery有一个有趣的json插件,叫做jquery json。
它允许您对JSON进行序列化和反序列化。工作原理如下:
1 2 3 4 5 6 7 | 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); |
你可以使用
1 | var object = $.parseJSON(jsonString); |
现在,您可以像访问本机javascript对象一样访问此对象:
1 | object.main.ProductsData.Product.BrandName // Joseph Joseph |
使用javascript:
然后用
可以使用jquery函数
如果jsonString存储了给定的字符串,
1 | jsonObj = $.parseJSON(jsonString); |
jsonObj.main.ProductsData.Product
将是一系列产品