我正在尝试访问JSON数据中的嵌套层,例如“ title”。但是,第一个属性是变量,并且其中的:
字符也无济于事
"ISBN:nnnnnnnnnnnn"
将值映射到要使用以下方法解构的组件:
renderBooks()
{
return this.state.books.map((book, index) =>
<BookDetail key={index} book={book} />);
}
render()
{
console.log(this.state);
return (
<ScrollView style={{ flex: 1 }}>
{this.renderBooks()}
</ScrollView>
);
}
组件:
const BookDetail = ({ book }) =>
{
const { title, author,... } = book;
return (
<Card>
<CardSection>
<View style={styles.headerContentStyle}>
<Text style={headerTextStyle}>{title}</Text>
<Text>{author}</Text>
</View>
</CardSection>
</Card>
);
};
JSON:
[
{
"ISBN:9780192853462": {
"publishers": [
{
"name": "Oxford University Press, USA"
}
],
"identifiers": {
"isbn_13": [
"9780192853462"
],
"openlibrary": [
"OL7384519M"
],
"isbn_10": [
"0192853465"
],
"librarything": [
"49548"
],
"goodreads": [
"334271"
]
},
"subtitle": "A Very Short Introduction (Very Short Introductions)",
"title": "Social and Cultural Anthropology",
"url": "https://openlibrary.org/books/OL7384519M/Social_and_Cultural_Anthropology",
"number_of_pages": 168,
"cover": {
"small": "https://covers.openlibrary.org/b/id/119856-S.jpg",
"large": "https://covers.openlibrary.org/b/id/119856-L.jpg",
"medium": "https://covers.openlibrary.org/b/id/119856-M.jpg"
},
"subjects": [
{
"url": "https://openlibrary.org/subjects/ethnology",
"name": "Ethnology"
}
],
"publish_date": "April 7, 2000",
"key": "/books/OL7384519M",
"authors": [
{
"url": "https://openlibrary.org/authors/OL656666A/John_Monaghan",
"name": "John Monaghan"
},
{
"url": "https://openlibrary.org/authors/OL2662612A/Peter_Just",
"name": "Peter Just"
}
],
"ebooks": [
{
"formats": {},
"preview_url": "https://archive.org/details/socialculturalan00mona",
"availability": "restricted"
}
]
}
}
答案 0 :(得分:0)
您可以自己制作book
对象的副本,所有图书信息都包装在ISBN
属性中,就我个人而言,我会把它做成book对象中的简单字段。
let data = {
"ISBN:9780192853462": {
"publishers": [
{
"name": "Oxford University Press, USA"
}
],
"identifiers": {
"isbn_13": [
"9780192853462"
],
"openlibrary": [
"OL7384519M"
],
"isbn_10": [
"0192853465"
],
"librarything": [
"49548"
],
"goodreads": [
"334271"
]
},
"subtitle": "A Very Short Introduction (Very Short Introductions)",
"title": "Social and Cultural Anthropology",
"url": "https://openlibrary.org/books/OL7384519M/Social_and_Cultural_Anthropology",
"number_of_pages": 168,
"cover": {
"small": "https://covers.openlibrary.org/b/id/119856-S.jpg",
"large": "https://covers.openlibrary.org/b/id/119856-L.jpg",
"medium": "https://covers.openlibrary.org/b/id/119856-M.jpg"
},
"subjects": [
{
"url": "https://openlibrary.org/subjects/ethnology",
"name": "Ethnology"
}
],
"publish_date": "April 7, 2000",
"key": "/books/OL7384519M",
"authors": [
{
"url": "https://openlibrary.org/authors/OL656666A/John_Monaghan",
"name": "John Monaghan"
},
{
"url": "https://openlibrary.org/authors/OL2662612A/Peter_Just",
"name": "Peter Just"
}
],
"ebooks": [
{
"formats": {},
"preview_url": "https://archive.org/details/socialculturalan00mona",
"availability": "restricted"
}
]
}
};
//let isbn = Object.keys(obj).find(item => item.match(/ISBN:\d/));
const destructureBook = (obj) => {
let book = {};
Object.keys(obj).map(function(key) {
if(key.match(/ISBN:\d/)) book['isbn'] = key;
Object.keys(obj[key]).map(field => book[field] = obj[key][field]);
});
return book;
}
// destructure the return of the function
const {isbn, title, publish_date } = destructureBook(data);
console.log(isbn, title, publish_date)