Parse Json in javascript or jquery [duplicate]

2020-07-18 06:06发布

Possible Duplicate:
how to parse json in javascript

I need to parse this JSON in JavaScript or jQuery. Please help me in getting the list of product in the below JSON.

To get the list of product

{
    "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条回答
爷、活的狠高调
2楼-- · 2020-07-18 06:14

Use the javascript: JSON.parse(yourjavascriptobjecttoparse);

Then var productx = yourjavascriptobjecttoparse['main']['ProductsData']['Product'] to reference the product.

查看更多
虎瘦雄心在
3楼-- · 2020-07-18 06:15

Use the parseJSON method in jQuery.

Example:

var obj = $.parseJSON(yourJsonString);
alert(obj.main.ProductsData.Product.Cost);
查看更多
倾城 Initia
4楼-- · 2020-07-18 06:16

Read JSON from file:

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

Or read JSON from string:

myobject = $.parseJSON(jsonString)

Now get the data you want:

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

Be sure to check your JSON data with http://jsonlint.com/

查看更多
Animai°情兽
5楼-- · 2020-07-18 06:27

There's an interesting JSON plugin for jQuery called jquery-json.

It allows you to both serialize and deserialize JSON. It works like this:

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);
查看更多
趁早两清
6楼-- · 2020-07-18 06:28

Just use JSON.parse function (MDN docu link)

yourObj = JSON.parse( jsonstring );

Afterwards you can access any property of the JSON string via your object, so, e.g.,

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

will return

"2-in-1 Measuring Jug"

in your given example.

查看更多
Luminary・发光体
7楼-- · 2020-07-18 06:32

You can use

var object = $.parseJSON(jsonString);

Now you can access this object like a native JavaScript Object:

object.main.ProductsData.Product.BrandName     // Joseph Joseph
查看更多
登录 后发表回答