How can I access JSON with jQuery?

I think this is a simple problem that I have not worked with JSON before.

Very simple, I have a JSON object:

fc_json = {
    "product_count": 1,
"total_price": 199.95,
"total_weight": 1,
"session_id": "26e8og4ldmlunj84uqf04l8l25",
    "custom_fields":{
        "affiliateID":"25"
    },
"messages":{
    "errors":[],
    "warnings":[],
    "info":[]
}
};

and I want to extract only the affiliateID variable using jQuery. Is there an easy way to do this? I really do not know.

+3
source share
2 answers

You do not need jQuery to access the JSON object. In fact, the JSON object is a JavaScript object.

You should be able to do

alert(fc_json.custom_fields.affiliateID) // alerts 25
+11
source

Like any other Javascript object, you can also access it:

fc_json['custom_fields']['affiliateID'] // Returns "25"
+2
source

Source: https://habr.com/ru/post/1712725/


All Articles