4

可能重复:
如何在 javascript 中解析 json

我需要用 JavaScript 或 jQuery 解析这个 JSON。请帮助我获取以下 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"
            }
        }
    }
}
4

7 回答 7

3

使用 jQuery 中的parseJSON方法

例子:

var obj = $.parseJSON(yourJsonString);
alert(obj.main.ProductsData.Product.Cost);
于 2012-04-26T09:23:37.603 回答
3

从文件中读取 JSON:

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

或者从字符串中读取 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/检查您的 JSON 数据

于 2012-04-26T09:30:51.023 回答
2

只需使用JSON.parse函数(MDN 文档链接

yourObj = JSON.parse( jsonstring );

之后,您可以通过您的对象访问 JSON 字符串的任何属性,例如,

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

将返回

"2-in-1 Measuring Jug"

在你给定的例子中。

于 2012-04-26T09:23:27.343 回答
0

您可以使用 jquery 函数$.parseJSON(jsonString)来获取 json 对象

如果 jsonString 存储您给定的字符串,

jsonObj = $.parseJSON(jsonString);

jsonObj.main.ProductsData.Product

将是一系列产品

于 2012-04-26T09:23:18.843 回答
0

使用 JavaScript:JSON.parse(yourjavascriptobjecttoparse);

然后var productx = yourjavascriptobjecttoparse['main']['ProductsData']['Product']参考产品。

于 2012-04-26T09:24:55.847 回答
0

您可以使用

var object = $.parseJSON(jsonString);

现在您可以像访问本机 JavaScript 对象一样访问此对象:

object.main.ProductsData.Product.BrandName     // Joseph Joseph
于 2012-04-26T09:26:11.150 回答
0

jQuery 有一个有趣的 JSON 插件,叫做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);
于 2012-04-26T09:37:39.147 回答