如何在 Javascript 中访问包含空值的 JSON 字符串
How to access JSON string containing null value in Javascript
我有这个 JSON 我从包含空值的 API 中检索。如何更好地遍历 JSON 并抽象功能?
const colletionjson =
{ "collections":
[ { "title" : "Home page"
, "description" : null
, "image" : null
}
, { "title" : "Products"
, "description" : "Products"
, "image" : { "src": "https:www.example.com" }
, "products_count" : 3
}
]
}
$.each(collectionjson, function (key, value) {
$.each(value, function (key, value) {
var collectionTitle = value.title;
var collectionDescription = value.description;
if (value == "image" && $.isArray(value)) {
var collectionImage = value.image.src;
} else {
var collectionImage = "";
}
});
});
// Do something with the data
if (colletionjson.collections && Array.isArray(colletionjson.collections)) {
colletionjson.collections.forEach(value => {
const collectionTitle = value.title;
const collectionDescription = value.description;
let collectionImage = '';
if (value.image && value.image.src) {
collectionImage = value.image.src;
// do something with this value
}
})
}
您现在可以使用 Optional chaining (?.) ECMAScript 2020
示例代码
const colletionjson = // just a name
{ collections:
[ { title : 'Home page'
, description : null
, image : null
}
, { title : 'Products'
, description : 'Products'
, image : { src : 'https:www.example.com' }
, products_count : 3
}
]
}
colletionjson.collections.forEach((collection, i) =>
{
let collectionTitle = collection.title
, collectionDescription = collection.description || ''
, collectionImage = collection.image?.src || ''
;
console.log (`index: ${i}, title: ${collectionTitle}, description: ${collectionDescription}, image: ${collectionImage}` )
})
我有这个 JSON 我从包含空值的 API 中检索。如何更好地遍历 JSON 并抽象功能?
const colletionjson =
{ "collections":
[ { "title" : "Home page"
, "description" : null
, "image" : null
}
, { "title" : "Products"
, "description" : "Products"
, "image" : { "src": "https:www.example.com" }
, "products_count" : 3
}
]
}
$.each(collectionjson, function (key, value) {
$.each(value, function (key, value) {
var collectionTitle = value.title;
var collectionDescription = value.description;
if (value == "image" && $.isArray(value)) {
var collectionImage = value.image.src;
} else {
var collectionImage = "";
}
});
});
// Do something with the data
if (colletionjson.collections && Array.isArray(colletionjson.collections)) {
colletionjson.collections.forEach(value => {
const collectionTitle = value.title;
const collectionDescription = value.description;
let collectionImage = '';
if (value.image && value.image.src) {
collectionImage = value.image.src;
// do something with this value
}
})
}
您现在可以使用 Optional chaining (?.) ECMAScript 2020
示例代码
const colletionjson = // just a name
{ collections:
[ { title : 'Home page'
, description : null
, image : null
}
, { title : 'Products'
, description : 'Products'
, image : { src : 'https:www.example.com' }
, products_count : 3
}
]
}
colletionjson.collections.forEach((collection, i) =>
{
let collectionTitle = collection.title
, collectionDescription = collection.description || ''
, collectionImage = collection.image?.src || ''
;
console.log (`index: ${i}, title: ${collectionTitle}, description: ${collectionDescription}, image: ${collectionImage}` )
})