在JavaScript或jQuery中解析Json

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"
            }
        }
    }
}
7个回答

3

使用jQuery中的parseJSON方法

示例:

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

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数据


不可以将文件名传递到 parseJSON 方法中。 - Guffa
@Guffa 不,我们可以。我曾经做过一次。你可以在这里看到它 https://dev59.com/NGLVa4cB1Zd3GeqPw4LD - Shinigamae

2

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

yourObj = JSON.parse( jsonstring );

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

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

将返回

"2-in-1 Measuring Jug"

在您提供的例子中。

0

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

如果jsonString存储了您提供的字符串,则可以这样使用

jsonObj = $.parseJSON(jsonString);

jsonObj.main.ProductsData.Product

将是一个产品数组


0

您可以使用

var object = $.parseJSON(jsonString);

现在你可以像访问本地JavaScript对象一样访问这个对象:

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

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

0
使用JavaScript:JSON.parse(yourjavascriptobjecttoparse); 然后使用var productx = yourjavascriptobjecttoparse['main']['ProductsData']['Product']来引用产品。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接