我有一个Firebase数据库,其中没有几个具有其属性的对象。
{
"products": [{
"sku": "70741",
"name": "Name",
"image": "name.png",
"quantity": 12,
"price": {
"value": 2.10,
"type": "simple"
},
"attributes": {
"0": "2",
"1": "4",
"2": "2|1|3",
"3": "1|2",
"4": "2",
"5": "6|7"
}
}
],
"attributes": [{
"id": 0,
"name": "name1",
"type": "type1"
},
{
"id": 1,
"name": "name2",
"type": "type2"
},
{
"id": 2,
"name": "name3",
"type": "type1"
}
],
"attributesIdValue": [
"companyName1|companyName2|companyName3|companyName4",
"typeName1|typeName2|typeName3|typeName4",
"address1|address2|address3|address4",
"1|2|3",
"country1|country2|country3|country4",
"number1|number2|number3|number4"
]}
products.attributes中的键应指向attributes.id和attributesIdValue.id,products.attributes中的值应根据其id和值指向attributesIdValue。
如何通过vue.js获得类似的信息:
product.attributes.0
应显示此产品的companyName
product.attributes.1
应显示此产品的typeName
product.attributes.2
应显示此产品的address
{{ product.price.value }}
工作并显示该值,但我无法获得这些属性。
-------编辑 换句话说,我想拥有这样的东西:
"products": [{
"sku": "70741",
"name": "Name",
"image": "name.png",
"quantity": 12,
"price": {
"value": 2.10,
"type": "simple"
},
"attributes": {
"attributes.0": "attributesIdValue.2",
"attributes.1": "attributesIdValue.4",
"attributes.2": "attributesIdValue.2|attributesIdValue.1|attributesIdValue.3",
"attributes.3": "attributesIdValue.1|attributesIdValue.2",
"attributes.4": "attributesIdValue.2",
"attributes.5": "attributesIdValue.6|attributesIdValue.7"
}
}
],
"attributes": [{
"id": 0,
"name": "name1",
"type": "type1"
},
{
"id": 1,
"name": "name2",
"type": "type2"
},
{
"id": 2,
"name": "name3",
"type": "type1"
}
],
"attributesIdValue": [
"companyName1|companyName2|companyName3|companyName4",
"typeName1|typeName2|typeName3|typeName4",
"address1|address2|address3|address4",
"1|2|3",
"country1|country2|country3|country4",
"number1|number2|number3|number4"
]}
这将向我输出其中的数据。
答案 0 :(得分:0)
您尝试过JSON.parse(data);
示例:
var data = '{ "products": [{ "sku": "70741", "name": "Name", "image": "name.png", "quantity": 12, "price": { "value": 2.10, "type": "simple" }, "attributes": { "0": "2", "1": "4", "2": "2|1|3", "3": "1|2", "4": "2", "5": "6|7" } } ], "attributes": [{ "id": 0, "name": "name1", "type": "type1" }, { "id": 1, "name": "name2", "type": "type2" }, { "id": 2, "name": "name3", "type": "type1" } ], "attributesIdValue": [ "companyName1|companyName2|companyName3|companyName4", "typeName1|typeName2|typeName3|typeName4", "address1|address2|address3|address4", "1|2|3", "country1|country2|country3|country4", "number1|number2|number3|number4" ]}';
obj = JSON.parse(data);
console.log(obj.attributes[0].name);