I'm trying to loop some products in JSON. I get many different kinds of JSON files and that's why I made form that I can just manually "patch" the file.
{ "product": [ { "@attributes": { "ID": "123456789" }, "name": "Name of the product"],...}
So I have two input fields in my form:
1. id="product_name" value="name"
2. id="product_id" value="['@attributes']['ID']"
I use ng-repeat to loop throug products. These work fine:
{{product}} //Product object
{{product.name}} //name of the product
{{product[value]}} //if value = name, name of the product
My problem is that I don't know how to get that ['@attribute']['ID'] from the product.
EDIT: I know that this will work:
{{product['@attributes']['ID']}}
but I need to change the value from form input.
EDIT: SOLUTION:
controller.getData = function(object, key) {
var keys = [];
var count = key.replace(/[^.]/g, '').length;
if(count === 4){
keys = key.split(".");
return object[keys[0]][keys[1]][keys[2]][keys[3]][keys[4]];
}else if(count === 3){
keys = key.split(".");
return object[keys[0]][keys[1]][keys[2]][keys[3]];
}else if(count === 2){
keys = key.split(".");
return object[keys[0]][keys[1]][keys[2]];
}else if(count === 1){
keys = key.split(".");
return object[keys[0]][keys[1]];
}else{
return object[key];
}
};
New problem:
{ "product": [
{ "@attributes": { "ID": "12345" },
"name": "productname",
"price": "xx",
"URL": "url",
"images": { "image": "imgUrl" },
"description": {},
"categories": {
"category": "Kesäale" },
"properties": { "property": [
{ "@attributes": { "name": "color" }, "value": "B25 Grisaille" },
{ "0": "\n", "@attributes": { "name": "size" } },
{ "@attributes": { "name": "currency" }, "value": "EUR" },
{ "@attributes": { "name": "brand" }, "value": "brandName" },
{ "@attributes": { "name": "fromPrice" }, "value": "xx" },
{ "@attributes": { "name": "manufacturer" }, "value": "xx" },
{ "@attributes": { "name": "weight" }, "value": "0.5" },
{ "@attributes": { "name": "stock" }, "value": "true" },
{ "@attributes": { "name": "EAN" }, "value": "1234" } ] },
"variations": {} },
How can I get brandName?