I need to retrieve information from a JSON-structure in Javascript I had this first:
<script type="text/javascript">
var jsonfile= '{"products":
{"productInfo1": "test",
"productInfo2": "test" }
}';
var digitalData = JSON.parse(jsonfile);
console.log(digtialData.products.productInfo1);
console.log(digtialData.products.productInfo2);
</script>
I get the output "test" in my console window. This worked fine. But it seems that I need to declare new objects, for proper use. As I understand, they don't like this solution. The method described above would be bad . So I tried to create a new script.
<script type="text/javascript">
var digitalData = new digitalData();
var product = new digitalData.product();
var productInfo1 = new digitalData.product.productInfo1();
var productInfo1 = new digitalData.product.productInfo2();
productInfo1 = "test";
productInfo2 = "test";
console.debug(digitalData.product.productInfo);
console.debug(digitalData.product.productInfo);
</script>
Then I get the error: "Uncaught TypeError: undefined is not a function index.php? route=product/product&path=57&product_id=49:265
(anonymous function)"
I tried various variants, and abused Google to find an answer how get this work. I found some answers, like to get digitalData.product working. But I'm looking to get the 3rd object working, and link that in any way to 1/2 objects (digitalData and product).
Anyone have suggestions how to solve this?
var digitalData = {};
instead? – SLoW Feb 14 at 14:36digitalData
before you can donew digitalData()
to create it. Same forproduct
andproductInfo
– Matt Burland Feb 14 at 14:43var digitalData = new digitalData();
overrides the original constructor. Use other variable name. – Shuhei Kagawa Feb 14 at 14:43